# check that data in balanced is not data=data[T/2] because this is wrong

# write.csv(conFF_lam_n,"conFF_lam_n_T24.csv",row.names = FALSE)
# Code problem: migliorare uso di deletestocks.
#     adesso o non si tolgono mai assets o ad ogni step se ne toglie almeno 1.

# better to open with RStudio
# session > set working directory > to source file location
# tools > install packages > parallel
# to compact the code and facilitate its use, click on the little arrow on lines 65, 350, 410, 472, 500, 519

################
# RUNNING CODE #
################
# run from line 61 to line 341 (comparira' un NULL nella finestra di console, non e' niente.)

# Una volta lanciata la parte empirica in output ci saranno le seguenti serie storiche
# ts.nstocks    -> serie storica nel numero di stock disponibile in quella finestra temporale
# ts.enddate    -> serie storica delle date di fine periodo delle varie window
# ts.valuef     -> matrice che contiene la serie storica dei vettori colonna contenenti i valori della funzione obiettivo per ciascun numero di fattori
# ts.eigenv     -> matrice che contiene la serie storica dei vettori colonna contenenti gli eigenvalues
# ts.loadmean   -> matrice che iene la serie storica dei vettori colonna le medie dei loading
# ts.loadvar    -> matrice che contiene la serie storica dei vettori colonna le varianze dei loading (diagonale si Sigma)
# ts.factors    -> array che contiene la serie storica delle matrici contenenti i fattori (ts.factors[,,i] per estrarre la matrice dei fattori della i-esima finestra)
# ts.nfactorhat -> serie storica del numero di fattori stimato secondo il criterio NEW1
# ts.sigma4
# ts.gamma2pass

# in 4) the output is:
# - vectors with the SDF, its variance, its upper and lower bound of a 95% CI
# - matrices with risk premia, their variance, their upper and lower bound of a 95% CI
# - plots of SDF and first 3 risk premia with their CI

# Contenuto di ogni elemento della lista "supp" (ogni elemento della lista corrisponde ad un timestep):
# 1) N stocks                   (scalare)
# 2) start period date          (scalare)
# 3) end period date            (scalare)
# 4) valore della funzione obiettivo per k = 1, ..., 5  (vettore)
# 5) i primi 5 autovalori  di X'X/NT                    (vettore)
# 6) media dei loadings per k = 1, ..., 5               (matrice triangolare k x n.fattori)
# 7) matrice di varianza e covarianza                   (array k x k x n.fattori)
# 8) fattori per k = 1, ..., 5                          (array T x k x n.fattori)
# 9) sigma4 per k = 1, ..., 5                           (vettore)
# 10) gamma_2pass                                       (matrice triangolare k x n.fattori)
# Se invece di salvare le quantita' fino a 5 fattori si vogliono salvare fino a 10, e' sufficiente scrivere nk = 10 nelle righe di "input parameters"

library(Rfast)
library(matrixcalc)
library(psych)
library(MASS)
library(car)
library(ggplot2)
#library(fastmatrix)
#library(MVT)
library(expm)
library("parallel")
cl <- makeCluster(detectCores() - 2)


# FUNCTIONS and OTHERS

nw24=480-24
nw60=480-60
nw120=480-120


# FUNCTIONS and OTHERS
{
  V = function(X, k) 
  {
    TT = nrow(X)
    N = ncol(X)
    factor = sqrt(TT) * as.matrix(eigen(X %*% t(X))$vectors[,1:k])
    factor = factor * (rep(1, TT) %*% t(sign(apply(factor, 2, mean))))
    lambda = t(1/TT *(t(factor) %*% X))
    out = 1/(N*TT) * sum((X - factor %*% t(lambda))^2)
    return(out)
  }
  V_exp = function(X, nk) 
  {
    TT = nrow(X)
    N = ncol(X)
    
    start = rownames(X)[1]
    end = rownames(X)[TT]
    eig = eigen(1/(TT*N) * X %*% t(X))$values[1:nk]
    
    val = rep(NA, nk)
    m_lambda = matrix(NA, ncol = nk, nrow = nk)
    s_lambda = matrix(NA, ncol = nk, nrow = nk)
    factor = matrix(NA, ncol = nk, nrow = TT)
    sigma_4 = rep(NA, nk)
    gamma_twopass = matrix(NA, ncol = nk, nrow = nk)
    
    for (k in 1:nk)
    {
      temp = sqrt(TT) * as.matrix(eigen(1/(TT*N) * X %*% t(X))$vectors[,1:k])
      temp = temp * (rep(1, TT) %*% t(sign(apply(temp, 2, mean))))
      lambda = t(1/TT *(t(temp) %*% X))
      m_lambda[1:k, k] = apply(lambda, 2, mean)
      val[k] = 1/(N*TT) * sum((X - temp %*% t(lambda))^2)
      temp1 = sum(diag((temp %*% t(temp))/TT)^2)
      temp1 = 3 + 27/TT*temp1 + 18*k/TT
      temp2 = 1/(N*TT) * sum((X - temp %*% t(lambda))^4)
      sigma_4[k] = temp2/temp1
      gamma_twopass[1:k,k] = solve(t(lambda) %*% lambda) %*% t(lambda) %*% apply(X, 2, mean)
    }
    factor[1:TT, 1:k] = temp
    s_lambda[1:k, 1:k] = (t(lambda) %*% lambda)/N
    
    out = list(N, start, end, val, eig, m_lambda, s_lambda, factor, sigma_4, gamma_twopass)
    names(out) = c("N", "start_date", "end_date", "value_function", "eigenvalues", "mean_loadings", "sigma_loadings", "factors", "sigma_4", "gamma_twopass")
    return(out)
  }
  criteria = function(X, k, kmax, eps1, eps2) 
  {
    TT = nrow(X)
    N = ncol(X)
    out1 = V(X, k)
    out2 = V(X, kmax)
    
    out3 = k * ((N+TT)/(N*TT)) * log((N*TT)/(N+TT))
    PC_P1 = out1 + out2 * out3
    IC_P1 = log(out1) + out3
    
    out3 = k * ((N+TT)/(N*TT)) * log(min(N,TT))
    PC_P2 = out1 + out2 * out3
    IC_P2 = log(out1) + out3
    
    out3 = k * log(min(N,TT))/min(N,TT)
    PC_P3 = out1 + out2 * out3
    IC_P3 = log(out1) + out3
    
    out3 = k * 2/TT
    AIC1 = out1 + out2 * out3
    
    out3 = k * log(TT)/TT
    BIC1 = out1 + out2 * out3
    
    out3 = k * 2/N
    AIC2 = out1 + out2 * out3
    
    out3 = k * log(N)/N
    BIC2 = out1 + out2 * out3
    
    out3 = k * 2 * ((N + TT - k)/(N*TT))
    AIC3 = out1 + out2 * out3
    
    out3 = k * ((N + TT - k)*log(N*TT)/(N*TT))
    BIC3 = out1 + out2 * out3
    
    out = c(PC_P1, PC_P2, PC_P3, IC_P1, IC_P2, IC_P3, AIC1, BIC1, AIC2, BIC2, AIC3, BIC3)
    
    for (i in 1:length(eps2))
    {
      out3 = k * ((log(N))^eps1 * N^eps2[i])/sqrt(N)
      NEW1 = out1 + out3
      NEW2 = log(out1) + out3
      NEW3 = out1 + out2 * out3
      NEW4 = TT/(TT-k)*out1 + out3
      out = c(out, NEW1, NEW2, NEW3, NEW4)
    }
    return(c(out, out1))
  }
  f = function(i, data, rf.tot, window, ncol = NA, kmax, eps1 = NA, eps2 = NA, std = F, extract = F, ts.deletestocks = NA) 
  {
    X = data[i:(i + window - 1), ]
    rf = rf.tot[i:(i + window - 1)] - 1
    if (is.na(ts.deletestocks[i]))
    {
      delete = c()
      for (j in 1:ncol(X)) if (sum(is.na(X[,j])) > 0) delete = c(delete, j)
      if (class(delete) != "NULL") X = X[,-delete]
    }
    if (!is.na(ts.deletestocks[i]))
    {
      delete = ts.deletestocks[[i]]
      if (class(delete) != "NULL") X = X[,-delete]
    }
    
    X = as.matrix(X)
    N = ncol(X)
    X = X - rf %*% t(rep(1, N))
    if (std == T)
    {
      m = apply(X, 2, mean)
      s = apply(X, 2, sd)
      X = (X - rep(1, window) %*% t(m))/(rep(1, window) %*% t(s))
    }
    
    if (extract)
    {
      out = V_exp(X, kmax)
      return(out)
    }
    
    start = rownames(X)[1]
    end = rownames(X)[window]
    
    temp = matrix(NA, nrow = kmax, ncol = ncol - 3)                   # ncol - 2
    for (k in 1:kmax) temp[k,] = criteria(X, k, kmax, eps1, eps2)
    out = rep(NA, ncol - 3)                                           # ncol - 2
    for (l in 1:(ncol - 3)) out[l] = which(temp[,l] == min(temp[,l])) # ncol - 2
    return(list(c(start, end, N, out), delete))                       # N and delete
  }

  f2 = function(i, data, rf.tot, window, ncol = NA, kmax, eps1 = NA, eps2 = NA, std = F, extract = F, ts.deletestocks = NA) 
  {
    X = data[i:(i + window - 1), ]
    rf = rf.tot[i:(i + window - 1)] - 1
    # if (is.na(ts.deletestocks[i]))
    # {
    #   delete = c()
    #   for (j in 1:ncol(X)) if (sum(is.na(X[,j])) > 0) delete = c(delete, j)
    #   if (class(delete) != "NULL") X = X[,-delete]
    # }
     if (!is.na(ts.deletestocks[i]))
    {
      delete = ts.deletestocks[[i]]
      if (class(delete) != "NULL") X = X[,-delete]
    }
    
    X = as.matrix(X)
    N = ncol(X)
    X = X - rf %*% t(rep(1, N))
    if (std == T)
    {
      m = apply(X, 2, mean)
      s = apply(X, 2, sd)
      X = (X - rep(1, window) %*% t(m))/(rep(1, window) %*% t(s))
    }
    
    if (extract)
    {
      out = V_exp(X, kmax)
      return(out)
    }
    
    start = rownames(X)[1]
    end = rownames(X)[window]
    
    temp = matrix(NA, nrow = kmax, ncol = ncol - 3)                   # ncol - 2
    for (k in 1:kmax) temp[k,] = criteria(X, k, kmax, eps1, eps2)
    out = rep(NA, ncol - 3)                                           # ncol - 2
    for (l in 1:(ncol - 3)) out[l] = which(temp[,l] == min(temp[,l])) # ncol - 2
    return(list(c(start, end, N, out), delete))                       # N and delete
  }
    factor_time_series = function(supp)
  {
    TT = dim(supp[[1]]$factors)[1]
    nk = dim(supp[[1]]$factors)[2]
    nw = length(supp)
    
    ts.nstocks = rep(NA, nw)
    ts.enddate = rep(NA, nw)
    ts.valuef = matrix(NA, nrow = nk, ncol = nw)
    ts.eigenv = matrix(NA, nrow = nk, ncol = nw)
    ts.loadmean = matrix(NA, nrow = nk, ncol = nw)
    ts.loadvar = array(NA, dim = c(nk, nk, nw))
    ts.factors = array(NA, dim = c(TT, nk, nw))
    ts.sigma4 = matrix(NA, nrow = nk, ncol = nw)
    ts.gamma2pass = array(NA, dim = c(nk, nk, nw))
    
    for (i in 1:nw)
    {
      ts.nstocks[i] = supp[[i]]$N
      ts.enddate[i] = supp[[i]]$end_date
      ts.valuef[,i] = supp[[i]]$value_function
      ts.eigenv[,i] = supp[[i]]$eigenvalues
      ts.loadmean[,i] = supp[[i]]$mean_loadings[,nk]
      ts.loadvar[,,i] = supp[[i]]$sigma_loadings
      ts.factors[,,i] = supp[[i]]$factors
      ts.sigma4[,i] = supp[[i]]$sigma_4
      ts.gamma2pass[,,i] = supp[[i]]$gamma_twopass
    }
    
    for (i in 1:length(ts.enddate))
    {
      m = substr(ts.enddate[i], 2, 3)
      d = substr(ts.enddate[i], 5, 6)
      y = substr(ts.enddate[i], 8, 11)
      ts.enddate[i] = paste(d, "-", m, "-", y, sep = "")
    }
    ts.enddate = as.Date(ts.enddate, format = "%d-%m-%Y")
    
    out = list(ts.nstocks, ts.enddate, ts.valuef, ts.eigenv, ts.loadmean, ts.loadvar, ts.factors, ts.sigma4, ts.gamma2pass)
    return(out)
  }
  
  commutation_matrix = function(r, c)
  {
    er = diag(1, r)
    ec = diag(1, c)
    
    out = matrix(0, ncol = r*c, nrow = r*c)
    for (i in 1:r)
      for (j in 1:c)
      {
        e1 = as.matrix(er[,i])
        e2 = as.matrix(ec[,j])
        out = out + (e1 %*% t(e2)) %x% (e2 %*% t(e1))
      }
    return(out)
  }
  SDF_fun = function(rf, k, factors)
  {
    TT = nrow(factors)
    
    factors = as.matrix(factors[,1:k])
    gamma = apply(factors, 2, mean)
    
    omega = diag(1, k) #var(factors)  #diag(1, k) - gamma %*% t(gamma)
    omega = diag(1, k) - gamma %*% t(gamma)
    
        omega_inv = solve(omega)
    out =  1/rf - (1/rf)*t(gamma) %*% omega_inv %*% t(factors - rep(1, TT) %*% t(gamma))
    #out = out[TT]
    
    return(c(out))
  }
  
  
  SDF_vol = function(rf, k, factors)
  {
    TT = nrow(factors)
    
    factors = as.matrix(factors[,1:k])
    gamma = apply(factors, 2, mean)
    
    omega = diag(1, k) - gamma %*% t(gamma)
    omega_inv = solve(omega)
    #out =  1/rf - 1/rf*t(gamma) %*% omega_inv %*% t(factors - rep(1, TT) %*% t(gamma))
    #out = out[TT]
    out=(t(gamma) %*% omega_inv %*%gamma)/(rf*rf)
    
    return(out)
  }
  
  
  SDF_volFF = function(rf, k, factors)
  {
    TT = nrow(factors)
    
    factors = as.matrix(factors[,1:k])
    gamma = apply(factors, 2, mean)
    
    omega = cov(factors)#diag(1, k) - gamma %*% t(gamma)
    omega_inv = solve(omega)
    #out =  1/rf - 1/rf*t(gamma) %*% omega_inv %*% t(factors - rep(1, TT) %*% t(gamma))
    #out = out[TT]
    out=(t(gamma) %*% omega_inv %*%gamma)/(rf*rf)
    
    return(out)
  }
  
  
  SDFu_fun = function(rf, k, factors,gammakk,omegakk)
  {
    TT = nrow(factors)
    
    factors = as.matrix(factors[,1:k])
    #  gamma = apply(factors, 2, mean)
    gammaa=gammakk#[1:k,]
    omegaa=omegakk#[1:k,1:k]
    
    
    # omega = diag(1, k) - gamma %*% t(gamma)
    omega_inv = solve(omegaa)
    out =  1/rf - (1/rf)*t(gammaa) %*% omega_inv %*% t(factors - rep(1, TT) %*% t(gammaa))
    #out = out[TT]
    
    return(c(out))
    
  }
  
  
  SDFu_vol = function(rf, k, factors, gammakk, omegakk)
  {
    #TT = nrow(factors)
    
    factors = as.matrix(factors[,1:k])
    #  gamma = apply(factors, 2, mean)
    gammaa=gammakk#[1:k,]
    omegaa=omegakk#[1:k,1:k]
    
    
    
    omega_inv = solve(omegaa)
    out=(t(gammaa)%*%omega_inv%*%gammaa)/(rf*rf)
    return(out)
    
  }
  
  
  SDF_corr = function( k, factors,gammakk,omegakk)
  {
    #TT = nrow(factors)
    
    factors = as.matrix(factors[,1:k])
    gamma = apply(factors, 2, mean)
    gammaa=gammakk#[1:k,]
    omegaa=omegakk#[1:k,1:k]
    
    
    #omega = diag(1, k) - gamma %*% t(gamma)
    omega_inv = solve(cov(factors))
    
    omegaa_inv = solve(omegaa)
    
    
    p1= sqrt( (t(gammaa) %*% omegaa_inv %*% gammaa ) )
    p2= sqrt( (t(gamma) %*% omega_inv %*% gamma ) )
    
    
    out=(t(gamma) %*% omegaa_inv %*% gammaa )/(p1*p2)
    return(c(out))
    
  }
  
  
  SDFFF_corr = function( k, factors,gammakk,omegakk)
  {
    #TT = nrow(factors)
    
    factors = as.matrix(factors[,1:k])
    gamma = apply(factors, 2, mean)
    gammaa=gammakk[1:k,]
    omegaa=omegakk[1:k,1:k]
    
    
    #omega = diag(1, k) - gamma %*% t(gamma)
    omega_inv = solve(cov(factors))
    
    omegaa_inv = solve(omegaa)
    
    p1= sqrt( (t(gammaa) %*% omegaa_inv %*% gammaa ) )
    p2= sqrt( (t(gamma) %*% omega_inv %*% gamma ) )
    
    #  is.nan(gammaa[1])
    #  
    #         if (is.nan(gammaa[1])==T) {print(10)}
    #   if (is.nan(gammaa[1])==T) {   out=(t(gamma) %*% omega_inv  %*% gammaa )/(p1*p2)} else
    # # {    out=(t(gamma) %*% sqrtm(omega_inv) %*% sqrtm(omegaa_inv)  %*% gammaa )/(p1*p2)}
    # 
    out=(t(gamma) %*% sqrtm(omega_inv) %*% sqrtm(omegaa_inv)  %*% gammaa )/(p1*p2)
    
    return(c(out))
    
  }
  
  
  SDF_acm_fun = function(rf, k, factors, eigenvalue, valuef, sigma4)
  {
    TT = nrow(factors)
    t = TT
    
    factors = as.matrix(factors[,1:k])
    valuef = valuef[k]
    eigenvalue = eigenvalue[1:k]
    sigma4 = sigma4[k]
    
    IvecT = matrix(1, ncol = 1, nrow = TT)/TT
    ImatT = diag(1, TT)
    Imatk = diag(1, k)
    M_1T = ImatT - IvecT %*% solve(t(IvecT) %*% IvecT) %*% t(IvecT)
    K_rT = commutation_matrix(k, TT)
    K_Tr = t(K_rT)  # commutation_matrix(TT, k)
    
    gamma = as.matrix(apply(factors, 2, mean))
    omega = diag(1, k) - gamma %*% t(gamma)
    omega_inv = solve(omega)
    sigma2 = TT/(TT - k)*valuef
    U = diag(eigenvalue, k) - sigma2/TT*Imatk
    U_inv = solve(U)
    sigma_lambda = U
    #Ue = diag(sigma4, TT^2)
    # for (j in 1:TT)
    #  for (k in 1:TT)
    #    Ue[(j - 1)*TT + k, (k - 1)*TT + j] = Ue[(j - 1)*TT + k, (k - 1)*TT + j] + sigma4
    #Ue = sigma4*( vec(diag(window))%*%t(vec(diag(window))) + diag(TT^2) )
    D1 = (IvecT %x% Imatk) %*% omega_inv %*% (t(factors) %*% (ImatT[, t] - IvecT))
    D2 = ((ImatT[, t] - IvecT) %x% Imatk) %*% omega_inv %*% (t(factors) %*% IvecT)
    D31 = (1/TT * M_1T %*% factors %*% omega_inv %*% t(factors)) %x% (omega_inv %*% t(factors))
    D32 = (IvecT %x% (ImatT[, t] - IvecT)) + ((ImatT[, t] - IvecT) %x% IvecT)
    D = 1/rf[t]*(- D1 - D2 + D31 %*% D32)
    
    supp = (U_inv %*% (t(factors)/TT))
    supp2=supp%*%t(supp)
    # supp = (ImatT %x% supp) 
    E1=sigma4*( vec(supp)%*%t(vec(supp)) + (ImatT%x%supp2))
    #  E1 = supp %*% Ue %*% t(supp)
    # time = Sys.time()
    # E1 = (ImatT %x% (U_inv %*% (t(factors)/TT))) %*% Ue %*% (ImatT %x% ((factors/TT) %*% U_inv))
    # Sys.time() - time
    E2 = sigma2*ImatT %x% U_inv
    E3 = (factors %*% sigma_lambda %*% t(factors)) %x% (sigma2/TT*U_inv^2)
    E4 = ((sigma2/TT*factors %*% U_inv) %x% t(factors)) %*% K_rT
    E5 = t(E4) # K_Tr %*% ((sigma2/TT*U_inv %*% t(factors)) %x% factors)
    E = E1 + E2 + E3 + E4 + E5
    
    out = (t(D) %*% E %*% D)
    return(out)
  }
  RiskPremia_fun = function(k, factors)
  {
    factors = as.matrix(factors[,1:k])
    out = apply(factors, 2, mean)
    return(out)
  }
  RiskPremia_acm_fun = function(k, factors, eigenvalue, valuef, sigma4)
  {
    TT = nrow(factors)
    
    factors = as.matrix(factors[,1:k])
    valuef = valuef[k]
    eigenvalue = eigenvalue[1:k]
    sigma4 = sigma4[k]
    
    Imatk = diag(1, k)
    gamma = as.matrix(apply(factors, 2, mean))
    sigma2 = TT/(TT - k)*valuef
    U = diag(eigenvalue, k) - sigma2/TT*Imatk
    U_inv = solve(U)
    Sigma_lambda = U
    
    A = rbind(Imatk, Imatk, Imatk)
    C11 = sigma4/TT^2*U_inv^2 + sigma4/TT^2*U_inv %*% gamma %*% t(gamma) %*% U_inv
    C22 = sigma2/TT*U_inv
    C23 = sigma2/TT*U_inv %*% Sigma_lambda %*% gamma %*% t(gamma) %*% U_inv
    C32 = t(C23)
    C33 = sigma2/TT*c((t(gamma) %*% Sigma_lambda %*% gamma))*U_inv^2
    C12 = C13 = C21 = C31 = diag(0, k)
    C1 = cbind(C11, C12, C13)
    C2 = cbind(C21, C22, C23)
    C3 = cbind(C31, C32, C33)
    C = rbind(C1, C2, C3)
    
    out = t(A) %*% C %*% A
    return(out)
  }
  SDF_RP_parallel = function(i,ts.deletestocks , window, lambdahalf, gammahalf, omegahalf, X_balanced ,  data , rf, ts.nfactorhat, ts.factors,  ts.factorsu, ts.eigenv, ts.valuef, ts.sigma4 , gammak , omegak , FF , gammaaFF, omegaaFF)
  {
    
    
    #  pseudo oos SDF for uncond  PCA
    
    #data_iwindow1=X_balanced[i+window-1,]
     fhalf=matrix(0,10,1)
     Thalf=as.integer(nrow(X_balanced))# bad name: this is T of bal
     Nhalf=as.integer(ncol(X_balanced))# bad name: this is N of bal
     
    # X_balanced=as.matrix(X_balanced,Thalf,Nhalf)
     lambdahalf=  as.matrix(lambdahalf,Nhalf,10)
     lambdahalf = sqrt(Nhalf) * as.matrix(eigen(1/(0.5*Thalf*Nhalf) * t(X_balanced[1:(0.5*Thalf),]) %*% (X_balanced[1:(0.5*Thalf),]))$vectors[,1:10])
     # 
     ftemphalf=t(X_balanced[1:(0.5*Thalf),]%*%lambdahalf)/Nhalf 
     
  
 
     omegahalf=var(t(ftemphalf))
     gammahalf  = apply( ftemphalf,1,mean)    
 
     omegahalf= as.matrix(omegahalf,10,10)
     gammahalf  = as.matrix(gammahalf,10,1)
     # if (nfac==1)
     #          {     gammahalfconun  = mean( ftemphalf) }  else {
     #            gammahalfconun  = apply( ftemphalf,2,mean)        }
     #          
     
        # ii=as.numeric(i)
    # window=as.integer(window)
    # i=as.integer(i)
#     inwin1=0
  # iwin1=as.numeric(i+window-1)
  # fhalf=t(lambdahalf[1:Nhalf,])%*%(X_balanced[(i+window-1),1:Nhalf])%/%Nhalf # this is pseudo-factor at time i+win-1
     fhalf=t(X_balanced[(i+window-1),]%*%lambdahalf)/Nhalf # this is pseudo-factor at time i+win-1
      # fhalf=t(matrix(2,1,Nhalf) %*% lambdahalf[1:Nhalf,1:10])#/as.integer(Nhalf) # this is pseudo-factor at time i+win-1
     
  #    fhalf=as.matrix(fhalf,10,1)
   sdfhalf=matrix(0,10,1)
     j=1
      for (j in 1:10) 
      {  
 omegahalf_inv=as.matrix(solve(omegahalf[1:j,1:j]),j,j)
        sdfhalf[j]= 
          #t(gammahalf[1:j,1]) %*% omegahalf_inv %*% (fhalf[1:j,1] - gammahalf[1:j,1])/rf[7]
        1/rf[(i+window-1)] - (1/rf[(i+window-1)])*t(gammahalf[1:j,1]) %*% omegahalf_inv %*% (fhalf[1:j,1] - gammahalf[1:j,1])
      }
     
   #  qf=(1/rf[(i+window-1)])*t(gammahalf[1:j,1]) %*% omegahalf_inv %*% (fhalf[1:j,1] - gammahalf[1:j,1])
     
    
     #  this construct the pseduo oos SD -  caso balanced
     # and  pseudo oos SDF unconditional (same timing)
     {
       
       # wrong bal cond oos
        {
       # nfac = ts.nfactorhat[i-1]
       # Nhalf=as.integer(ncol(X_balanced))
       # #H=matrix(rnorm(nfac^2,0,1),nfac,nfac)
       # 
       # Fbal=matrix(0,window,nfac)
       # Fbal = sqrt(window) * as.matrix(eigen(1/(window*Nhalf) * X_balanced[(i-1):(i+window-2),] %*% t(X_balanced[(i-1):(i+window-2),]))$vectors[,1:nfac])
       #  
       #   
       # 
       # 
       #   factorsimeno1 =Fbal# matrix(ts.factors[,1:nfac,i-1],window,nfac)
       # 
       # 
       #   #data_iwindow1=X_balanced[i+window-1,]
       #   fhalfcon=matrix(0,nfac,1)
       #   #Thalf=as.integer(nrow(X_balanced))
       #  
       # 
       #   # X_balanced=as.matrix(X_balanced,Thalf,Nhalf)
       #   lambdahalfcon= t(X_balanced[(i-1):(i+window-2),])%*%factorsimeno1/window  #t(X_balanced[(i+window-1),]%*%lambdahalf)/Nhalf
       # 
       #   # fhalf=t(lambdahalf[1:Nhalf,])%*%(X_balanced[(i+window-1),1:Nhalf])%/%Nhalf # this is pseudo-factor at time i+win-1
       #   fhalfcon=t(X_balanced[(i+window-1),]%*%lambdahalfcon)/Nhalf # this is pseudo-factor at time i+win-1
       #   totfac=rbind(as.matrix(factorsimeno1[2:window,1:nfac],window-1,nfac),t(fhalfcon))
       #   
       #   
       #   
       #   
       #   
       # 
       #   omega=t(totfac)%*%totfac/window
       #   totfac=totfac%*%solve(sqrtm(omega))
       #   
       #   omegahalfcon= var(totfac)
       #   
       #   
       #   
       #   
       #   
       #  if (nfac==1)
       #   {     gammahalfcon  = mean(totfac) }  else { 
       #     gammahalfcon  = apply(totfac,2,mean)        }
       #   
       #   
       #   
       #   
       #     sdfhalfcon=0  # this is oos for i+w-1 baded on i-1:i+w-2
       #     omegahalfcon_inv=solve(omegahalfcon)
       #     sdfhalfcon=       1/rf[(i+window-1)] - (1/rf[(i+window-1)])*t(gammahalfcon) %*% omegahalfcon_inv %*% matrix((totfac[window,1:nfac] - gammahalfcon),nfac,1)
       #     zeromean=matrix(0,10,1)
       #     zeromean[1:nfac]=(fhalfcon - gammahalfcon)
       #     gammamean=matrix(0,10,1)
       #     gammamean[1:nfac]=gammahalfcon
       #     omegamean=matrix(0,10,1)
       #     omegamean[1:nfac,1]=matrix(as.matrix(diag(omegahalfcon)),nfac,1)
       #     omegadet=0
       #     omegadet=det(omegahalfcon)
        }
           # with lambal
        nfac = ts.nfactorhat[i-1]
        Nhalf=as.integer(ncol(X_balanced))
       
        # nfacx1
        
           lambal=matrix(0,Nhalf,nfac)
           lambal = sqrt(Nhalf) * as.matrix(eigen(1/(window*Nhalf) * t(X_balanced[(i-1):(i+window-2),]) %*% (X_balanced[(i-1):(i+window-2),]))$vectors[,1:nfac])
           Fbal2= (X_balanced[(i-1):(i+window-2),])%*%lambal/Nhalf #t(X_balanced[(i+window-1),]%*%lambdahalf)/Nhalf
           
           fhalfcon2=t(X_balanced[(i+window-1),]%*%lambal)/Nhalf # this is pseudo-factor at time i+win-1
           totfac2=rbind(as.matrix(Fbal2[2:window,1:nfac],window-1,nfac),t(fhalfcon2))
           totfac2=matrix(totfac2,window,nfac)
     
           
           # 10x1
           lambal_vec=matrix(0,Nhalf,10)
           lambal_vec = sqrt(Nhalf) * as.matrix(eigen(1/(window*Nhalf) * t(X_balanced[(i-1):(i+window-2),]) %*% (X_balanced[(i-1):(i+window-2),]))$vectors[,1:10])
           Fbal2_vec= (X_balanced[(i-1):(i+window-2),])%*%lambal_vec/Nhalf #t(X_balanced[(i+window-1),]%*%lambdahalf)/Nhalf
           
           fhalfcon2_vec=t(X_balanced[(i+window-1),]%*%lambal_vec)/Nhalf # this is pseudo-factor at time i+win-1
           totfac2_vec=rbind(as.matrix(Fbal2_vec[2:window,1:10],window-1,10),t(fhalfcon2_vec))
           totfac2_vec=matrix(totfac2_vec,window,10)
           
           # versione due
           
           lambal_vec2=matrix(0,Nhalf,10)
           lambal_vec2 = sqrt(Nhalf) * as.matrix(eigen(1/(window*Nhalf) * t(X_balanced[(i-1):(i+window-1),]) %*% (X_balanced[(i-1):(i+window-1),]))$vectors[,1:10])
           Fbal2_vec2= (X_balanced[(i-1):(i+window-2),])%*%lambal_vec2/Nhalf #t(X_balanced[(i+window-1),]%*%lambdahalf)/Nhalf
           
           fhalfcon2_vec2=t(X_balanced[(i+window-1),]%*%lambal_vec2)/Nhalf # this is pseudo-factor at time i+win-1
           totfac2_vec2=rbind(as.matrix(Fbal2_vec2[2:window,1:10],window-1,10),t(fhalfcon2_vec2))
           totfac2_vec2=matrix(totfac2_vec2,window,10)
           
           
           
#           cbind(apply(Fbal2_vec[2:window,1:10],2,mean),fhalfcon2_vec)
                 
           
       #    omega2=t(totfac2)%*%totfac2/window
      #     totfac2=totfac2%*%solve(sqrtm(omega2))
           
           omegahalfcon2= var(totfac2)
           omegahalfcon2_vec= var(totfac2_vec)
           omegahalfcon2_vec2= var(totfac2_vec2)
           
           
           
           
           
           if (nfac==1)
           {     gammahalfcon2  = mean(totfac2) }  else { 
             gammahalfcon2  = apply(totfac2,2,mean)        }
           gammahalfcon2_vec  = apply(totfac2_vec,2,mean) 
           gammahalfcon2_vec2  = apply(totfac2_vec2,2,mean) 
           
           
           
           
           sdfhalfcon=0  # this is oos for i+w-1 baded on i-1:i+w-2
           omegahalfcon2_inv=solve(omegahalfcon2)
           sdfhalfcon=       1/rf[(i+window-1)] - (1/rf[(i+window-1)])*t(gammahalfcon2) %*% omegahalfcon2_inv %*% matrix((totfac2[window,] - gammahalfcon2),nfac,1)
           
           
           
           
           sdfhalfcon_vec=matrix(0,10,1)  # this is oos for i+w-1 baded on i-1:i+w-2
           a=matrix(0,window,10)  # this is oos for i+w-1 baded on i-1:i+w-2
    #       b=matrix(0,10,1)  # this is oos for i+w-1 baded on i-1:i+w-2
    #       c=matrix(0,10,1)  # this is oos for i+w-1 baded on i-1:i+w-2
           d=matrix(0,10,1)  # this is oos for i+w-1 baded on i-1:i+w-2
           
           # omegahalfcon2_inv=solve(omegahalfcon2)
        j=1
        for (j in 1:10)
        {
          
           sdfhalfcon_vec[j]=       1/rf[(i+window-1)] - (1/rf[(i+window-1)])*t(gammahalfcon2_vec[1:j])%*% solve(omegahalfcon2_vec[1:j,1:j])  %*% matrix((totfac2_vec[window,1:j] - gammahalfcon2_vec[1:j]),j,1)
#            a[,j]=       1/rf[(i+window-1)] - (1/rf[(i+window-1)])*t(gammahalfcon2_vec[1:j])%*% solve(omegahalfcon2_vec[1:j,1:j])  %*% t(matrix((totfac2_vec[,1:j] - matrix(1,window,1)%*%t(gammahalfcon2_vec[1:j])),window,j))
#            
# if (j==1)
# {  d[j]=       1/rf[(i+window-1)] - (1/rf[(i+window-1)])* mean(Fbal2_vec[,1]) %*% solve(var(Fbal2_vec[,1:j])) %*% (Fbal2_vec[window,1:j]  - mean(Fbal2_vec[,1:j]))}
#          else
# {    d[j]=    1/rf[(i+window-1)] - (1/rf[(i+window-1)])*   t( apply(Fbal2_vec[,1:j],2,mean)) %*% solve(var(Fbal2_vec[,1:j])) %*% (Fbal2_vec[window,1:j]  - apply(Fbal2_vec[,1:j],2,mean))}
    
           }  
          # delete
        # cbind(sdfhalfcon_vec,t(a),d)
        # cbind(sdfhalfcon_vec,(a[5,]),d)
        # 
        # plot(sdfhalfcon_vec)
        # points(d,col=2)
        # points(b,col=3)
        # points(c,col=4)
        
          
        sdfhalfcon_vec2=matrix(0,10,1)  # this is oos for i+w-1 baded on i-1:i+w-2
        #a=matrix(0,window,10)  # this is oos for i+w-1 baded on i-1:i+w-2
        #       b=matrix(0,10,1)  # this is oos for i+w-1 baded on i-1:i+w-2
        #       c=matrix(0,10,1)  # this is oos for i+w-1 baded on i-1:i+w-2
        #d=matrix(0,10,1)  # this is oos for i+w-1 baded on i-1:i+w-2
        
      #   omegahalfcon2_inv=solve(omegahalfcon2)
        j=1
        for (j in 1:10)
        {
          
          sdfhalfcon_vec2[j]=       1/rf[(i+window-1)] - (1/rf[(i+window-1)])*t(gammahalfcon2_vec2[1:j])%*% solve(omegahalfcon2_vec2[1:j,1:j])  %*% matrix((totfac2_vec2[window,1:j] - gammahalfcon2_vec2[1:j]),j,1)
          #            a[,j]=       1/rf[(i+window-1)] - (1/rf[(i+window-1)])*t(gammahalfcon2_vec[1:j])%*% solve(omegahalfcon2_vec[1:j,1:j])  %*% t(matrix((totfac2_vec[,1:j] - matrix(1,window,1)%*%t(gammahalfcon2_vec[1:j])),window,j))
          #            
          # if (j==1)
          # {  d[j]=       1/rf[(i+window-1)] - (1/rf[(i+window-1)])* mean(Fbal2_vec[,1]) %*% solve(var(Fbal2_vec[,1:j])) %*% (Fbal2_vec[window,1:j]  - mean(Fbal2_vec[,1:j]))}
          #          else
          # {    d[j]=    1/rf[(i+window-1)] - (1/rf[(i+window-1)])*   t( apply(Fbal2_vec[,1:j],2,mean)) %*% solve(var(Fbal2_vec[,1:j])) %*% (Fbal2_vec[window,1:j]  - apply(Fbal2_vec[,1:j],2,mean))}
          
        }  
        
        
        
           
           #qfc=(1/rf[(i+window-1)])*t(gammahalfcon2) %*% omegahalfcon2_inv %*% matrix((totfac2[window,] - gammahalfcon2),nfac,1)
           
           fhalfcon2=rbind(fhalfcon2,matrix(0,(10-nfac),1))
           Fbal2=cbind(Fbal2,matrix(0,window,(10-nfac)))
           
           # zeromean2=matrix(0,10,1)
           # zeromean2[1:nfac]=(totfac2[window,] - gammahalfcon2)
           # gammamean2=matrix(0,10,1)
           # gammamean2[1:nfac]=gammahalfcon2
           # omegamean2=matrix(0,10,1)
           # omegamean2[1:nfac,1]=matrix(as.matrix(diag(omegahalfcon2)),nfac,1)
           # omegadet2=0
           # omegadet2=det(omegahalfcon2)
            
           
           #zeromean[1]=t(gammahalfcon) %*% omegahalfcon_inv %*% matrix((fhalfcon - gammahalfcon),nfac,1)
           # dummy
           {
          #  fdummy=ts.factors[window,1:nfac,i]
          #  totfacd=ts.factors[,1:nfac,i]
          # # totfacd=matrix(rnorm(window*nfac,0,1),window,nfac)
          # # fdummy=totfacd[1,]
          #  
          #       omegahalfcond= var(totfacd)
          #  
          #  
          #  
          #  if (nfac==1)
          #  {     gammahalfcond  = mean(totfacd) }  else { 
          #    gammahalfcond  = apply(totfacd,2,mean)        }
          #  
          #  
          #  
          #  
          #  
          #  sdfhalfcondummy=0  # this is oos for i+w-1 baded on i-1:i+w-2
          #  omegahalfcond_inv=as.matrix(solve(omegahalfcond),nfac,nfac)
          #  sdfhalfcondummy=       1/rf[(i+window-1)] - (1/rf[(i+window-1)])*t(gammahalfcond) %*% omegahalfcond_inv %*% (fdummy - gammahalfcond)
          #  
          #  zeromean1=matrix(0,10,1)
          #  zeromean1[1:nfac]=(fdummy - gammahalfcond)
           }          
             #t(gammahalfcond) %*% omegahalfcond_inv %*% (fdummy - gammahalfcond)#
             
        # IMPORTANT: check by using gamma and omega of the pseudo-oos factors!
     }
     #end of   oos
     
     # construct pseudo oos caso unbalanced 
     {
     index = (i-1):(i + window - 1)
     X = data[index, ]

     #  sdfuu=sdfu[index,1]
     #  sdfuuFF=sdfuFF[index,1]

     deleteall=unique(c(ts.deletestocks[[i-1]],ts.deletestocks[[i]]))

     if (!is.na(ts.deletestocks[[1]][1]))
     {
       delete = deleteall #ts.deletestocks[[i-1]]
       if (class(delete) != "NULL") X = X[,-delete]
       #if (class(delete) != "NULL") lambdau=lambdau.supp[-delete,]
       #if (class(delete) != "NULL") lambdauFF=lambdau.FF[-delete,]

       #if (class(delete) != "NULL") sdfuu=sdfu[-delete,1]


     }
     X = as.matrix(X)
     N = ncol(X)
     TT=nrow(X)
     rf.supp = rf[index] - 1
     X = X - rf.supp %*% t(rep(1, N))

     X_mean = apply(X, 2, mean)


     lambalunbal=matrix(0,N,nfac)
     lambalunbal = sqrt(N) * as.matrix(eigen(1/(window*N) * t(X[1:(TT-1),]) %*% (X[1:(TT-1),]))$vectors[,1:nfac])
     Fbal2unbal= (X[1:(TT-1),])%*%lambalunbal/N #t(X_balanced[(i+window-1),]%*%lambdahalf)/Nhalf
     fhalfcon2unbal=t(X[TT,]%*%lambalunbal)/N # this is pseudo-factor at time i+win-1
     
     
     totfac2unbal=rbind(as.matrix(Fbal2unbal[2:window,1:nfac],window-1,nfac),t(fhalfcon2unbal))
     totfac2unbal=matrix(totfac2unbal,window,nfac)

     
     lambalunbal_vec=matrix(0,N,10)
     lambalunbal_vec = sqrt(N) * as.matrix(eigen(1/(window*N) * t(X[1:(TT-1),]) %*% (X[1:(TT-1),]))$vectors[,1:10])
     Fbal2unbal_vec= (X[1:(TT-1),])%*%lambalunbal_vec/N #t(X_balanced[(i+window-1),]%*%lambdahalf)/Nhalf
     fhalfcon2unbal_vec=t(X[TT,]%*%lambalunbal_vec)/N # this is pseudo-factor at time i+win-1
     
     
     totfac2unbal_vec=matrix(NA,window,10)
     totfac2unbal_vec=rbind(Fbal2unbal_vec[2:window,],t(fhalfcon2unbal_vec))
     totfac2unbal_vec=matrix(totfac2unbal_vec,window,10)
     
     

   ##  factor.supp = as.matrix(ts.factors[,1:nfac,(i-1)],window,nfac)
  ##   lambda.supp = t(1/window *(t(factor.supp) %*% X[1:(TT-1),]))


##      fhalfconunbal=t(X[TT,]%*%lambda.supp)/N # this is pseudo-factor at time i+win-1
##      totfac=rbind(as.matrix(factor.supp[2:window,1:nfac],window-1,nfac),t(fhalfconunbal))

      omegahalfconunbal= var(totfac2unbal)
      omegahalfconunbal_vec= var(totfac2unbal_vec)
      
      if (nfac==1)
      {     gammahalfconun  = mean(totfac2unbal) }  else {
        gammahalfconun  = apply(totfac2unbal,2,mean)        }

      gammahalfconun_vec  = apply(totfac2unbal_vec,2,mean)      
     
      
      
     sdfhalfconunbal=0  # this is oos for i+w-1 baded on i-1:i+w-2
     omegahalfconunbal_inv = as.matrix(solve(omegahalfconunbal),nfac,nfac)
     sdfhalfconunbal=     1/rf[(i+window-1)] - (1/rf[(i+window-1)])*t(gammahalfconun) %*% omegahalfconunbal_inv %*% (fhalfcon2unbal  - gammahalfconun)

     
       sdfhalfconunbal_vec=matrix(NA,10,1)  # this is oos for i+w-1 baded on i-1:i+w-2
  #     omegahalfconunbal_vec_inv = as.matrix(solve(omegahalfconunbal_vec),10,10)
       # STRANO: it increases with j?
       j=1
       for (j in 1:10)
       {
       sdfhalfconunbal_vec[j]=     1/rf[(i+window-1)] - (1/rf[(i+window-1)])*t(gammahalfconun_vec[1:j]) %*% solve(omegahalfconunbal_vec[1:j,1:j]) %*% (fhalfcon2unbal_vec[1:j]  - gammahalfconun_vec[1:j])
       }
     # to complete the window x 10 matrix  
          fhalfcon2unbal=rbind(fhalfcon2unbal,matrix(0,(10-nfac),1))

     Fbal2unbal=cbind(Fbal2unbal,matrix(0,window,(10-nfac)))


     }
     
     # end of oos
    
    nfac = ts.nfactorhat[i]
    nfacFF=5
    factors = ts.factors[,,i]
    window = nrow(factors)
    factorsFF = FF[i:(i + window - 1),]
    #mean(factors)
    factorsu = ts.factorsu[,,1]
    eigenvalue = ts.eigenv[,i]
    valuef = ts.valuef[,i]
    sigma4 = ts.sigma4[,i]
    gammaa=gammak[1:nfac,]
    omegaa=omegak[1:nfac,1:nfac]
    
#    1/rf[i+window-1] + (1/rf[i+window-1])*
    
    sdf = SDF_fun(rf[i:(i + window - 1)], nfac, factors) 
   
    sdf_vec=matrix(NA,window,10)
   
    j=1
   for (j in 1:10)
   {
     sdf_vec[,j] = SDF_fun(rf[i:(i + window - 1)],j,factors) 
     
   }
    
    
    
    
     sdfu = SDFu_fun(rf,nfac, factorsu,gammaa,omegaa)
  
     TTT=nrow(factorsu)
  
     sdfu_vec=matrix(NA,TTT,10)

     
     j=1
     for (j in 1:10)
     {
       
       
       gammaa=gammak[1:j,]
       omegaa=omegak[1:j,1:j]
       
       sdfu_vec[,j] =  SDFu_fun(rf,j, factorsu,gammaa,omegaa)
  #       SDF_fun(rf[i:(i + window - 1)],j,factors) 
       
     }
     
     
     sdf_var_vec=matrix(NA,10,1)
     
     j=1
     for (j in 1:10)
     {
       
     #  eigenvalue = ts.eigenv[,i]
    #   valuef = ts.valuef[,i]
    #   sigma4 = ts.sigma4[,i]
       
       
       
       sdf_var_vec[j] = SDF_acm_fun(rf[i:(i + window - 1)], j, factors, eigenvalue, valuef, sigma4)
#         SDFu_fun(rf,j, factorsu,gammaa,omegaa)
       #       SDF_fun(rf[i:(i + window - 1)],j,factors) 
       
     }
     
     
      sdf_var = SDF_acm_fun(rf[i:(i + window - 1)], nfac, factors, eigenvalue, valuef, sigma4)
    
      
      
      gammaa=gammak[1:nfac,]
      omegaa=omegak[1:nfac,1:nfac]
      
    risk_premia = RiskPremia_fun(nfac, factors)
    risk_premia_var = RiskPremia_acm_fun(10, factors, eigenvalue, valuef, sigma4)
    sdf_vol=SDF_vol(rf[(i+window-1):(i + window - 1)], nfac, factors)
    #sdfu_vol=SDFu_vol(rf[(i+window-1):(i + window - 1)], nfac, factors, gammaa, omegaa)
    sdf_corr=SDF_corr( nfac, factors, gammaa , omegaa )
    
    
    sdfFF = SDF_fun(rf[i:(i + window - 1)], nfacFF, factorsFF)
    sdfuFF = SDFu_fun(rf,nfacFF,FF,gammaaFF,omegaaFF)
    sdf_varFF = 0 # SDF_acm_fun(rf[i:(i + window - 1)], nfacFF, factorsFF, eigenvalue, valuef, sigma4)
    
    risk_premiaFF = RiskPremia_fun(nfacFF, factorsFF)
    risk_premia_varFF = 0#RiskPremia_acm_fun(nfac, factors, eigenvalue, valuef, sigma4)
    sdf_volFF=SDF_volFF(rf[(i+window-1):(i + window - 1)], nfacFF, factorsFF)
    #sdfu_vol=SDFu_vol(rf[(i+window-1):(i + window - 1)], nfac, factors, gammaa, omegaa)
    sdf_corrFF=SDFFF_corr( nfacFF, factorsFF, gammaaFF , omegaaFF )
    
    
    
    #            1   2     3           4               5           6       7         8   9         10        11              12              13          14       15        16          17          18     19     20             21        22             23              24          25                   25           26        27        28                         
    out = list(sdf,sdfu, sdf_var, risk_premia, risk_premia_var,sdf_vol,sdf_corr,sdfFF,sdfuFF, sdf_varFF, risk_premiaFF, risk_premia_varFF,sdf_volFF,sdf_corrFF,sdfhalf, sdfhalfcon,sdfhalfconunbal,fhalf,fhalfcon2,fhalfcon2unbal,Fbal2,Fbal2unbal,sdfhalfcon_vec,sdfhalfconunbal_vec,sdf_vec,
            #  26           27             28             29        30              31
               Fbal2_vec,fhalfcon2_vec,Fbal2_vec2,fhalfcon2_vec2,Fbal2unbal_vec,fhalfcon2unbal_vec,
   #    32        33
   sdfu_vec,sdf_var_vec)#,qf,qfc)#,sdfhalfcondummy, zeromean, zeromean1,gammamean,omegamean,omegadet,sdfhalfcon2,zeromean2,gammamean2,omegamean2,omegadet2)
    
               
               
               
               
               
    return(out)
    
    
    
    
    
    
    
    
    
    
    # QQ
    # sdfFF[,i] = supp[[i]][[8]]1
    # sdfuFF[,i] = supp[[i]][[9]]2
    # 
    # 
    # sdfTFF[i] = sdfFF[window, i]
    # sdfT_varFF[i] = supp[[i]][[10]]3 
    # 
    # risk_premiaFF[1:5, i] = supp[[i]][[11]]4
    # risk_premia_covFF[1:5, 1:5, i] = supp[[i]][[12]]5
    # risk_premia_varFF[1:5, i] = diag(supp[[i]][[13]])6
    # sdf_volFF[,i] = supp[[i]][[14]]7
    # 
    # 
    # omega_invFF = solve(omegakFF[1,1])
    # rr=rf[i+window-1]
    # sdfu_volFF[,i] = (t(gammaFF[1,])%*%omega_inv%*%gammak[1,])/(rr*rr)
    # 
    # 
    # sdfFF_corr[,i] = supp[[i]][[14]]7
    # QQ
    
  }
 
  # this makes the uncon pseudo oos
  
#   SDFU_RP_parallel = function(i, gammahalf, omegahalf,  data_iwindow1 ,  rf, ts.nfactorhat, ts.factors,  ts.factorsu, ts.eigenv, ts.valuef, ts.sigma4 , gammak , omegak , FF , gammaaFF, omegaaFF)
   {
#     
#     fhalf=t(lambdahalf)%*%data_iwindow1 # this is pseudo-factor at time i+win-1
#     
#     j=1
#     for (j in 1:10) 
# {    sdfhalf[j]= 1/rf[(i + window - 1)] - 1/rf[(i + window - 1)]*t(gammahalf[1:j]) %*% solve(omegahalf[1:j,1:j]) %*% (fhalf[1:j] - gammahalf[1:j])}
#     
#       
#     #   rf[i:(i + window - 1)],
#     # 
#     # nfac = ts.nfactorhat[i]
#     # nfacFF=5
#     # factors = ts.factors[,,i]
#     # window = nrow(factors)
#     # factorsFF = FF[i:(i + window - 1),]
#     # 
#     # factorsu = ts.factorsu[,,1]
#     # eigenvalue = ts.eigenv[,i]
#     # valuef = ts.valuef[,i]
#     # sigma4 = ts.sigma4[,i]
#     # gammaa=gammak[1:nfac,]
#     # omegaa=omegak[1:nfac,1:nfac]
#     # 
#     # 
#     # 
#     # sdf = SDF_fun(rf[i:(i + window - 1)], nfac, factors)
#     # sdfu = SDFu_fun(rf,nfac, factorsu,gammaa,omegaa)
#     # sdf_var = SDF_acm_fun(rf[i:(i + window - 1)], nfac, factors, eigenvalue, valuef, sigma4)
#     # 
#     # risk_premia = RiskPremia_fun(nfac, factors)
#     # risk_premia_var = RiskPremia_acm_fun(10, factors, eigenvalue, valuef, sigma4)
#     # sdf_vol=SDF_vol(rf[(i+window-1):(i + window - 1)], nfac, factors)
#     # #sdfu_vol=SDFu_vol(rf[(i+window-1):(i + window - 1)], nfac, factors, gammaa, omegaa)
#     # sdf_corr=SDF_corr( nfac, factors, gammaa , omegaa )
#     # 
#     # 
#     # sdfFF = SDF_fun(rf[i:(i + window - 1)], nfacFF, factorsFF)
#     # sdfuFF = SDFu_fun(rf,nfacFF,FF,gammaaFF,omegaaFF)
#     # sdf_varFF = 0 # SDF_acm_fun(rf[i:(i + window - 1)], nfacFF, factorsFF, eigenvalue, valuef, sigma4)
#     # 
#     # risk_premiaFF = RiskPremia_fun(nfacFF, factorsFF)
#     # risk_premia_varFF = 0#RiskPremia_acm_fun(nfac, factors, eigenvalue, valuef, sigma4)
#     # sdf_volFF=SDF_volFF(rf[(i+window-1):(i + window - 1)], nfacFF, factorsFF)
#     # #sdfu_vol=SDFu_vol(rf[(i+window-1):(i + window - 1)], nfac, factors, gammaa, omegaa)
#     # sdf_corrFF=SDFFF_corr( nfacFF, factorsFF, gammaaFF , omegaaFF )
#     # 
#     # 
#     
#     #            1   2     3           4               5           6       7         8   9         10        11              12              13          14
# #    out = list(sdf,sdfu, sdf_var, risk_premia, risk_premia_var,sdf_vol,sdf_corr,sdfFF,sdfuFF, sdf_varFF, risk_premiaFF, risk_premia_varFF,sdf_volFF,sdf_corrFF)
# out=sdfhalf
#         return(out)
#     
#     
#     
#     
#     
#     
#     
#     
#     
#     
#     # QQ
#     # sdfFF[,i] = supp[[i]][[8]]1
#     # sdfuFF[,i] = supp[[i]][[9]]2
#     # 
#     # 
#     # sdfTFF[i] = sdfFF[window, i]
#     # sdfT_varFF[i] = supp[[i]][[10]]3 
#     # 
#     # risk_premiaFF[1:5, i] = supp[[i]][[11]]4
#     # risk_premia_covFF[1:5, 1:5, i] = supp[[i]][[12]]5
#     # risk_premia_varFF[1:5, i] = diag(supp[[i]][[13]])6
#     # sdf_volFF[,i] = supp[[i]][[14]]7
#     # 
#     # 
#     # omega_invFF = solve(omegakFF[1,1])
#     # rr=rf[i+window-1]
#     # sdfu_volFF[,i] = (t(gammaFF[1,])%*%omega_inv%*%gammak[1,])/(rr*rr)
#     # 
#     # 
#     # sdfFF_corr[,i] = supp[[i]][[14]]7
#     # QQ
#     
   }
   
  sel_fun = function(x)
  {
    out = sum(!is.na(x))
    return(out)
  }
  pe_fun = function(x)
  {
    out = mean(x, na.rm = T)
    return(out)
  }
  
  oos_parallel = function(i, balancedN , data, rf, window, windowu, ts.eigenv , ts.deletestocks, ts.factors, ts.valuef, ts.nfactorhat, nfactorhatu, k0 , gammak , omegak , lambdau , valuefu, FF ,lambdau.suppFF  , ts.sigma4 )
  {
    
    # QQQ
    #   #FF
    #   for (i in 1:(nw - 1)) ret.porttFF[, i + 1] = supp[[i]][[9+1]]
    #   for (i in 1:(nw - 1)) ret.portvFF[, i + 1] = supp[[i]][[9+2]]
    #   for (i in 1:(nw - 1)) ret.porttuFF[, i + 1] = supp[[i]][[9+3]]
    #   for (i in 1:(nw - 1)) ret.portvuFF[, i + 1] = supp[[i]][[9+4]]
    #   
    #   for (i in 1:(nw - 1))
    #   {ret.portv_rollFF[, i + 1] = supp[[i]][[9+5]]
    #   }
    #   
    #   for (i in 1:(nw - 1))
    #   {ret.portvu_rollFF[, i + 1] = supp[[i]][[9+6]]
    #   }
    #   for (i in 1:(nw-1)) SR.porttFF[i+1,]  =  supp[[i]][[9+7]]
    #   for (i in 1:(nw - 1)) SR.portvFF[i + 1,]  =  supp[[i]][[9+8]]
    #   for (i in 1:(nw - 1)) SR.porttuFF[i + 1,]  =  supp[[i]][[9+9]]
    #   SR.porttFF[1,]=matrix(0,nrow=1,ncol=7)
    #   SR.porttuFF[1,]=matrix(0,nrow=1,ncol=7)   
    #   SR.portvFF[1,]=matrix(0,nrow=1,ncol=7)  
    
    lambdauFF=lambdau.suppFF
    index = i:(i + window-1)
    index_oos = (i+window):(i + 2*window-1)
    
    rf.supp = rf[index] - 1
    rf.oos = rf[index_oos] - 1
    
    X = data[index,]
    X_oos=data[index_oos,]
    
    
    
    # if (!is.na(ts.deletestocks[[1]][1]))
    # {
    #   delete = ts.deletestocks[[i]]
    #   if (class(delete) != "NULL") X = X[,-delete_oos]
    # }
    
    # if (!is.na(ts.deletestocks[[1]][1]))
    # {
    #   delete_oos = ts.deletestocks[[(i+window)]]
    #   if (class(delete_oos) != "NULL") X_oos = X_oos[,-delete_oos]
    # }
    # 
    # 
    X_temp = data[c(index, index_oos),]
    delete_oos = apply(X_temp, 2, function(x){sum(is.na(x))})
    delete_oos = which(delete_oos > 0)
    
    
    X = X[,-delete_oos]
    
    N = ncol(X)
    X = as.matrix(X) - rf.supp %*% t(rep(1, N))
    
    
    X_oos = X_oos[,-delete_oos]
    
    N_oos = ncol(X_oos)
    X_oos = as.matrix(X_oos) - rf.oos %*% t(rep(1, N_oos))
    
    
    
    #N_oos = ncol(X_oos)
    #X_oos = as.matrix(X_oos) - rf.oos %*% t(rep(1, N_oos))
    
    
    
    
    X_in2=data[index,]-rf.supp%*%t(rep(1,ncol(data)))
    X.next2=data[i+window,]-(rf[i+window]-1)%*%t(rep(1,ncol(data)))
    
    lead=window+1
    X.next=matrix(NA,nrow=1,ncol=N)
    X.next = data[i+window,-delete_oos]-(rf[i+window]-1)%*%t(rep(1,N))
    X_in=X
    
    
    factor.supp = ts.factors[,,i]
    lambda.supp = t(1/window *(t(factor.supp) %*% X))
    
    risk_premia.supp = apply(factor.supp, 2, mean)
    valuef.supp = ts.valuef[, i]
    
    k = c(ts.nfactorhat[i], k0)
    ku = c(nfactorhatu, k0)
    #X.next = data[window + i,]
    
    # if (!is.na(ts.deletestocks[[1]][1]))
    #X.next =matrix(X.next[-delete],nrow=1,ncol=N)
    #X.next =X.next[-delete]
    redistribute = is.na(X.next)
    
    
    
    # if (!is.na(ts.deletestocks[[1]][1]))    
    #X_in=X_in[,-delete]
    # #redistribute_is = is.na(X_in)
    N_in=ncol(X_in)
    N_next=ncol(X.next)
    
    
    
    outt = rep(NA, length(k) + 1)
    outv = rep(NA, length(k) + 1)
    outtu = rep(NA, length(k) + 1)
    outvu = rep(NA, length(k) + 1)
    outvu_roll = rep(NA, length(k) + 1)
    outv_roll = rep(NA, length(k) + 1)
    outtu2 = rep(NA, length(k) + 1)
    outvu2 = rep(NA, length(k) + 1)
    outvu_roll2 = rep(NA, length(k) + 1)
    outtu3 = rep(NA, length(k) + 1)
    outvu3 = rep(NA, length(k) + 1)
    outvu_roll3 = rep(NA, length(k) + 1)
    outtu4 = rep(NA, length(k) + 1)
    outvu4 = rep(NA, length(k) + 1)
    outvu_roll4 = rep(NA, length(k) + 1)
    
    
    
    SR_outt_is = rep(NA, length(k) + 1)
    SR_outv_is = rep(NA, length(k) + 1)
    SR_outtu_is = rep(NA, length(k) + 1)
    SR_outvu_is = rep(NA, length(k) + 1)
    SR_outtu_is2 = rep(NA, length(k) + 1)
    SR_outvu_is2 = rep(NA, length(k) + 1)
    
    
    SR_outvu_roll_is = rep(NA, length(k) + 1)
    SR_outv_roll_is = rep(NA, length(k) + 1)
    SR_outvu_roll_is2 = rep(NA, length(k) + 1)
    
    SR_outtu_is3 = rep(NA, length(k) + 1)
    SR_outvu_is3 = rep(NA, length(k) + 1)
    SR_outtu_is4 = rep(NA, length(k) + 1)
    SR_outvu_is4 = rep(NA, length(k) + 1)
    
    
    
    SR_outt_oos = rep(NA, length(k) + 1)
    SR_outv_oos = rep(NA, length(k) + 1)
    SR_outtu_oos = rep(NA, length(k) + 1)
    SR_outvu_oos = rep(NA, length(k) + 1)
    SR_outtu_oos2 = rep(NA, length(k) + 1)
    SR_outvu_oos2 = rep(NA, length(k) + 1)
    
    
    
    
    
    VaR_outt_is = rep(NA, length(k) + 1)
    ES_outt_is = rep(NA, length(k) + 1)
    
    VaR_outv_is = rep(NA, length(k) + 1)
    ES_outv_is = rep(NA, length(k) + 1)
    
    
    VaR_outt_oos = rep(NA, length(k) + 1)
    ES_outt_oos = rep(NA, length(k) + 1)
    
    VaR_outv_oos = rep(NA, length(k) + 1)
    ES_outv_oos = rep(NA, length(k) + 1)
    
    VaR_outtu_oos = rep(NA, length(k) + 1)
    ES_outtu_oos = rep(NA, length(k) + 1)
    
    VaR_outvu_oos = rep(NA, length(k) + 1)
    ES_outvu_oos = rep(NA, length(k) + 1)
    
    VaR_outtu_oos2 = rep(NA, length(k) + 1)
    ES_outtu_oos2 = rep(NA, length(k) + 1)
    
    VaR_outvu_oos2 = rep(NA, length(k) + 1)
    ES_outvu_oos2 = rep(NA, length(k) + 1)
    
    
    
    CI_SR_plus = rep(NA, length(k) + 1)
    CI_SR_minus = rep(NA, length(k) + 1)
    
    CI_VaR_plus = rep(NA, length(k) + 1)
    CI_VaR_minus = rep(NA, length(k) + 1)
    CI_ES_plus = rep(NA, length(k) + 1)
    CI_ES_minus = rep(NA, length(k) + 1)
    
    
    VaR_outtu_is = rep(NA, length(k) + 1)
    ES_outtu_is = rep(NA, length(k) + 1)
    VaR_outtu_oos = rep(NA, length(k) + 1)
    ES_outtu_oos = rep(NA, length(k) + 1)
    
    
    
    VaR_outtu_is2 = rep(NA, length(k) + 1)
    ES_outtu_is2 = rep(NA, length(k) + 1)
    VaR_outtu_is3 = rep(NA, length(k) + 1)
    ES_outtu_is3 = rep(NA, length(k) + 1)
    VaR_outtu_is4 = rep(NA, length(k) + 1)
    ES_outtu_is4 = rep(NA, length(k) + 1)
    
    
    VaR_outt_isFF = rep(NA,1)
    ES_outt_isFF = rep(NA,1)
    
    VaR_outv_isFF = rep(NA,1)
    ES_outv_isFF = rep(NA,1)
    
    VaR_outtu_isFF = rep(NA,1)
    ES_outtu_isFF = rep(NA,1)
    
    
    
    SR_outv_is2=rep(NA, length(k) + 1)
    VaR_outv_is2=rep(NA, length(k) + 1)
    ES_outv_is2=rep(NA, length(k) + 1)
    
    CI_SR_plus2=rep(NA, length(k) + 1)
    CI_SR_minus2=rep(NA, length(k) + 1)
    
    CI_VaR_plus2= rep(NA, length(k) + 1)
    CI_VaR_minus2= rep(NA, length(k) + 1)
    
    CI_ES_plus2=rep(NA, length(k) + 1)
    CI_ES_minus2=rep(NA, length(k) + 1)
    
    CI_SR_plus3=rep(NA, length(k) + 1)
    CI_SR_minus3=rep(NA, length(k) + 1)
    
    SR_outvu_is2=rep(NA, length(k) + 1)
    VaR_outvu_is2=rep(NA, length(k) + 1)
    ES_outvu_is2=rep(NA, length(k) + 1)
    
    VaR_outvu_is=rep(NA, length(k) + 1)
    ES_outvu_is=rep(NA, length(k) + 1)
    
    se_SR= rep(NA, length(k) + 1)
    se_VaR= rep(NA, length(k) + 1)
    se_ES= rep(NA, length(k) + 1)
    
    
    
    VaR_outv_is=rep(NA, length(k) + 1)
    ES_outv_is=rep(NA, length(k) + 1)
    
    VaR_outv_oos=rep(NA, length(k) + 1)
    ES_outv_oos=rep(NA, length(k) + 1)
    
    
    
    
    j=1  
    for (j in 1:length(ku))
      # j=1 
    {
      
      
      nfac = ku[j]
      
      
      # #massimo way start
      # omega = diag(1, nfac) - risk_premia.supp[1:nfac] %*% t(risk_premia.supp[1:nfac])
      # sigma2 = window/(window - nfac)*valuef.supp[nfac]
      # 
      # S = 1/sigma2*diag(1, N)
      # w2 = solve(solve(omega) + t(lambda.supp[,1:nfac]) %*% S %*% lambda.supp[,1:nfac])
      # temp = lambda.supp[,1:nfac]
      # temp = S %*% temp
      # w1 = S - temp %*% w2 %*% t(temp)
      # 
      # # per il peso devo premoltiplicare per l'inverse della covariance matrix dei returns
      # # che e' uguale a (S^-1 + AOmegaA'). La inverto con sherman-morrison/woodbury
      # w = w1 %*% lambda.supp[,1:nfac] %*% risk_premia.supp[1:nfac]
      ## massimo way end
      
      # tan port
      
      
      eigenvalue =ts.eigenv[1:nfac,i]# eigenvalue[1:nfac]
      sigma4 = ts.sigma4[nfac,i]
      sigma2 = window/(window - nfac)*valuef.supp[nfac]
      IvecT = matrix(1, ncol = 1, nrow = window)/window
      ImatT = diag(1, window)
      Imatk = diag(1, nfac)
      M_1T = ImatT - IvecT %*% solve(t(IvecT) %*% IvecT) %*% t(IvecT)
      if (nfac==1)          {    K_rT = diag(1,window)} else  
      {    K_rT = commutation_matrix(nfac, window)}
      K_Tr = t(K_rT)  # commutation_matrix(TT, k)
      
      # gamma = as.matrix(apply(factors, 2, mean))
      #  omega = diag(1, k) - gamma %*% t(gamma)
      #  omega_inv = solve(omega)
      #  sigma2 = TT/(TT - k)*valuef
      U = diag(eigenvalue[1:nfac], nfac) - (sigma2/window)*Imatk
      U_inv = solve(U)
      
      
      omega = diag(1, nfac) - risk_premia.supp[1:nfac] %*% t(risk_premia.supp[1:nfac])
      
      mulambda= t(lambda.supp[,1:nfac])%*%matrix(1,N,1)/N
      sigmalambda=(t(lambda.supp[,1:nfac]) %*% lambda.supp[,1:nfac]) - (N/window)*sigma2*diag(1,nfac)
      sigmalambda1=(t(lambda.supp[,1:nfac]) %*% lambda.supp[,1:nfac])
      w2 = solve(sigma2*solve(omega) + sigmalambda)
      
      aaa=( t(risk_premia.supp[1:nfac])%*%solve(omega)%*%w2%*%sigmalambda%*%w2%*%solve(omega)%*%risk_premia.supp[1:nfac])
      
      
      if(aaa<=0)     { sigmalambda=sigmalambda1}      
      
      w2=matrix(NA,nrow=nfac, ncol=nfac)
      
      w2 = solve(sigma2*solve(omega) + sigmalambda)
      w22 =  lambda.supp[,1:nfac] %*% w2 %*% solve(omega)%*%risk_premia.supp[1:nfac]
      w22[redistribute] = 0
      w_tan = w22/sum(w22)
      outt[j] = sum(w_tan[!redistribute]*X.next[!redistribute])
      
      gL= (w2%*%solve(omega)%*%risk_premia.supp[1:nfac])/as.numeric(sum(w22)) 
      
      rrr=matrix(NA,nrow=window,ncol=1)
      rrr= X_in[,!redistribute]%*%w_tan[!redistribute] - (N/window)*sigma2*factor.supp[,1:nfac]%*%gL
      SR_outt_is[j]=sqrt(12)*apply(rrr, 2, mean)/apply(rrr, 2, sd)
      
      rrr2=matrix(NA,nrow=window,ncol=1)
      rrr2= X_oos[,!redistribute]%*%w_tan[!redistribute]
      
      SR_outt_oos[j]=sqrt(12)*apply(rrr2, 2, mean)/apply(rrr2, 2, sd)
      
      
      p=0.05
      zp=qnorm(p)
      
      VaR_outt_is[j]=apply(rrr, 2, mean)+zp*apply(rrr, 2, sd)
      ES_outt_is[j]=apply(rrr, 2, mean)-(dnorm(zp)/p)*apply(rrr, 2, sd)
      
      VaR_outt_oos[j]=apply(rrr2, 2, mean)+zp*apply(rrr2, 2, sd)
      ES_outt_oos[j]=apply(rrr2, 2, mean)-(dnorm(zp)/p)*apply(rrr2, 2, sd)
      
      
      #   vol target 
      
      volp=sqrt( t(risk_premia.supp[1:nfac])%*%solve(omega)%*%w2%*%t(lambda.supp[,1:nfac])%*%
                   (lambda.supp[,1:nfac]%*%omega%*%t(lambda.supp[,1:nfac]) +sigma2*diag(N))%*%lambda.supp[,1:nfac]%*%
                   w2%*%solve(omega)%*%risk_premia.supp[1:nfac])
      tar_vol=0.05/sqrt(12)
      w_vol = (tar_vol)*w22%*%solve(volp)
      outv[j] = sum(w_vol[!redistribute]*X.next[!redistribute])
      
      gL= tar_vol*(w2%*%solve(omega)%*%risk_premia.supp[1:nfac])/as.numeric(volp)
      #      as.numeric((sqrt(t(risk_premia.supp[1:nfac])%*%solve(omega)%*%risk_premia.supp[1:nfac]))) 
      
      rrr=matrix(NA,nrow=window,ncol=1)
      rrr= X_in[,!redistribute]%*%w_vol[!redistribute] - (N/window)*sigma2*factor.supp[,1:nfac]%*%gL
      SR_outv_is[j]=sqrt(12)*apply(rrr, 2, mean)/apply(rrr, 2, sd)
      
      
      rrr2=matrix(NA,nrow=window,ncol=1)
      rrr2= X_oos[,!redistribute]%*%w_vol[!redistribute]
      SR_outv_oos[j]=sqrt(12)*apply(rrr2, 2, mean)/apply(rrr2, 2, sd)
      
      
      VaR_outv_is[j]=apply(rrr, 2, mean)+zp*apply(rrr, 2, sd)
      ES_outv_is[j]=apply(rrr, 2, mean)-(dnorm(zp)/p)*apply(rrr, 2, sd)
      
      VaR_outv_oos[j]=apply(rrr2, 2, mean)+zp*apply(rrr2, 2, sd)
      ES_outv_oos[j]=apply(rrr2, 2, mean)-(dnorm(zp)/p)*apply(rrr2, 2, sd)
      
      
      
      # VaR and ES
      {
        # 
        # 
        #     #  SE of rrr, SR, VaR , ES
        #     M=diag(window)-matrix(1,window,1)%*%matrix(1,1,window)/window
        #     if (nfac==1)
        #     {KK=diag(1,window)}  else
        # {    KK=commutation_matrix(window,nfac)}
        #     DeltaL=matrix(0,nrow=nfac,ncol=window*nfac+nfac^2+nfac+1)
        #     DeltaL[,1:(window*nfac)]=
        #       as.numeric(tar_vol/(volp*N)  )*
        #         kronecker(matrix(1,1,window)/window, U%*%solve(omega)%*%( diag(1,nfac) -
        #       as.numeric(tar_vol^2/(volp^2)  )*risk_premia.supp[1:nfac]%*%t(risk_premia.supp[1:nfac])%*%solve(omega)  )  )+
        #       as.numeric(tar_vol/(volp*N)  )*
        #       kronecker( t(risk_premia.supp[1:nfac])%*%solve(omega), U%*%solve(omega)%*%( -diag(1,nfac) +
        #               0.5*as.numeric(tar_vol^2/(volp^2)  )*risk_premia.supp[1:nfac]%*%t(risk_premia.supp[1:nfac])%*%solve(omega)  )  )%*%
        #       (   kronecker( t(factor.supp[,1:nfac])%*%M, diag(1,nfac) ) +   kronecker(diag(1,nfac),t(factor.supp[,1:nfac])%*%M) )
        # 
        # 
        # 
        #     #   as.numeric(tar_vol/(sigma2*volp*N)  )*(
        #     #         -kronecker( t(risk_premia.supp[1:nfac])%*%solve(omega),(2/window)*solve(sigmalambda/N)%*%
        #     #                       solve(omega)%*%t(factor.supp[,1:nfac])%*%M   )
        #     #   +kronecker(matrix(1,1,window),(1/window)*solve(sigmalambda/N)%*%solve(omega))%*%KK
        #     #   -kronecker(t(risk_premia.supp[1:nfac])%*%solve(omega),(1/as.numeric(window*volp^2))*solve(sigmalambda/N)%*%
        #     #                solve(omega)%*%risk_premia.supp[1:nfac]%*%
        #     #               (matrix(1,1,window)- t(risk_premia.supp[1:nfac])%*%solve(omega)%*%t(factor.supp[,1:nfac])%*%M ) )
        #     # )
        # 
        # 
        #     DeltaL[,(window*nfac+1):(window*nfac+nfac^2)]= -as.numeric(tar_vol/(volp*N)  )*
        #       kronecker(t(risk_premia.supp[1:nfac])%*%solve(omega)%*%U_inv,U_inv)
        #     DeltaL[,(window*nfac+nfac^2+1):(window*nfac+nfac^2+nfac)]=matrix(0,nfac,nfac)
        #         DeltaL[,(window*nfac+nfac^2+nfac):(window*nfac+nfac^2+nfac+1)]=matrix(0,nfac,1)
        #           # -as.numeric(tar_vol/(sigma2^2*volp*N)  )*
        #           # solve(sigmalambda/N)%*%solve(omega)%*%risk_premia.supp[1:nfac]
        #           #
        #         DeltaL2=matrix(0,nrow=nfac,ncol=(window*nfac+nfac^2+nfac+1))
        #         DeltaL2[,1:(window*nfac)]=kronecker(t(gL)%*%t(factor.supp[,1:nfac])/window,diag(1,nfac))
        # 
        #         DeltaL3=matrix(0,window*nfac+nfac^2+nfac+1,2*window*nfac+window^2+window)
        #         DeltaL3[1:(window*nfac),1:(window*nfac)]=diag(1,window*nfac)
        # 
        #         if (nfac==1)
        #         {K=1}      else
        #         {    K=commutation_matrix(nfac,nfac)}
        # 
        #         DeltaL3[(window*nfac+1):(window*nfac+nfac^2),1:(window*nfac)]=-(diag(1,nfac^2)+K)%*%
        #           kronecker(t(factor.supp[,1:nfac])/window,sigmalambda/N)
        #         DeltaL3[(window*nfac+1):(window*nfac+nfac^2),(window*nfac+1):(window*nfac+window^2)]=(
        #           kronecker(t(factor.supp[,1:nfac])/window,t(factor.supp[,1:nfac])/window)  - as.numeric(1/(window-nfac))*
        #             vec(t(factor.supp[,1:nfac])%*%factor.supp[,1:nfac]/window)%*%
        #             t(vec(diag(1,window)-factor.supp[,1:nfac]%*%t(factor.supp[,1:nfac])/window ))
        #         )
        #         DeltaL3[(window*nfac+1):(window*nfac+nfac^2),(window*nfac+window^2+1):(window*nfac+window^2+window*nfac)]=
        #           (diag(1,nfac^2)+K)%*%
        #           kronecker(t(factor.supp[,1:nfac])/window,diag(1,nfac))
        #         DeltaL3[(window*nfac+nfac^2+1):(window*nfac+nfac^2+nfac),1:(window*nfac)]=
        #           - kronecker(t(mulambda),t(factor.supp[,1:nfac])/window)%*%t(KK)
        #         DeltaL3[(window*nfac+nfac^2+1):(window*nfac+nfac^2+nfac),(2*nfac*window+window^2+1):(2*nfac*window+window^2+window)]=
        #           t(factor.supp[,1:nfac])/window
        #         DeltaL3[(window*nfac+nfac^2+nfac+1),(nfac*window+1):(nfac*window+window^2)]=
        #           as.numeric(1/(window-nfac))*t(vec(diag(1,window)-factor.supp[,1:nfac]%*%t(factor.supp[,1:nfac])/window))
        # 
        # 
        # 
        # 
        # 
        # 
        # 
        #         DeltaI=X_in%*%lambda.supp[,1:nfac]%*%( DeltaL - DeltaL2 )%*%DeltaL3
        # 
        #         DeltaII=matrix(0,(window),(2*window*nfac+window^2+window))
        #         DeltaII[,(window*nfac+1):(window*nfac+window^2)]= kronecker(N*t(gL)%*%t(factor.supp[,1:nfac])/window,
        #                                                                     diag(1,window)) -
        #           as.numeric(N/(window*(window-nfac)))*factor.supp[,1:nfac]%*%gL%*%t(vec(diag(1,window)-factor.supp[,1:nfac]%*%t(factor.supp[,1:nfac])
        #                                                                        /window))
        #         DeltaII[,(window*nfac+window^2+1):(window*nfac+window^2+window*nfac)]=
        #           kronecker(N*t(gL)%*%t(factor.supp[,1:nfac])/window,factor.supp[,1:nfac])
        # 
        # 
        #         VL=matrix(0,2*window*nfac+window^2+window,2*window*nfac+window^2+window)
        # 
        # 
        #           #TT = nrow(factors)
        #           #t = TT
        # 
        #          # factors = as.matrix(factors[,1:k])
        #         #  valuef = valuef[k]
        #           eigenvalue =ts.eigenv[1:nfac,i]# eigenvalue[1:nfac]
        #           sigma4 = ts.sigma4[nfac,i]
        # 
        #           IvecT = matrix(1, ncol = 1, nrow = window)/window
        #           ImatT = diag(1, window)
        #           Imatk = diag(1, nfac)
        #           M_1T = ImatT - IvecT %*% solve(t(IvecT) %*% IvecT) %*% t(IvecT)
        #           if (nfac==1)          {    K_rT = diag(1,window)} else
        #             {    K_rT = commutation_matrix(nfac, window)}
        #           K_Tr = t(K_rT)  # commutation_matrix(TT, k)
        # 
        #          # gamma = as.matrix(apply(factors, 2, mean))
        #         #  omega = diag(1, k) - gamma %*% t(gamma)
        #         #  omega_inv = solve(omega)
        #         #  sigma2 = TT/(TT - k)*valuef
        #           U = diag(eigenvalue[1:nfac], nfac) - (sigma2/window)*Imatk
        #           U_inv = solve(U)
        #         #  sigma_lambda = U
        #           # Ue = diag(sigma4, TT^2)
        #           #  for (j in 1:TT)
        #           #   for (k in 1:TT)
        #           #     Ue[(j - 1)*TT + k, (k - 1)*TT + j] = Ue[(j - 1)*TT + k, (k - 1)*TT + j] + sigma4
        #           Ue = sigma4*( vec(diag(window))%*%t(vec(diag(window))) + diag(window^2) )
        #           # D1 = (IvecT %x% Imatk) %*% solve(omega) %*% (t(factor.supp[,1:nfac]) %*% (ImatT[, t] - IvecT))
        #           # D2 = ((ImatT[, t] - IvecT) %x% Imatk) %*% solve(omega) %*% (t(factor.supp[,1:nfac]) %*% IvecT)
        #           # D31 = (1/TT * M_1T %*% factor.supp[,1:nfac] %*% solve(omega) %*% t(factor.supp[,1:nfac])) %x%
        #           #   (solve(omega) %*% t(factor.supp[,1:nfac]))
        #           # D32 = (IvecT %x% (ImatT[, t] - IvecT)) + ((ImatT[, t] - IvecT) %x% IvecT)
        #           # D = 1/rf[t]*(- D1 - D2 + D31 %*% D32)
        #           #
        #           supp = (U_inv%*%(t(factor.supp[,1:nfac])/window))
        #           supp2=supp%*%t(supp)
        #           # supp = (ImatT %x% supp)
        #           E1=sigma4*( vec(supp)%*%t(vec(supp)) + (ImatT%x%supp2))
        #            # E1 = supp %*% Ue %*% t(supp)
        #           # time = Sys.time()
        #           # E1 = (ImatT %x% (U_inv %*% (t(factors)/TT))) %*% Ue %*% (ImatT %x% ((factors/TT) %*% U_inv))
        #           # Sys.time() - time
        #           E2 = sigma2*ImatT %x% U_inv
        #           E3 = (factor.supp[,1:nfac] %*%U%*% t(factor.supp[,1:nfac])) %x% (sigma2/window*(U_inv)^2)
        #           E4 = ((sigma2/window*factor.supp[,1:nfac] %*%U_inv) %x% t(factor.supp[,1:nfac])) %*% K_rT
        #           E5 = t(E4) # K_Tr %*% ((sigma2/TT*U_inv %*% t(factors)) %x% factors)
        #           E = E1 + E2 + E3 + E4 + E5
        # 
        #           VL=matrix(0,2*window*nfac+window^2+window,2*window*nfac+window^2+window)
        #           VL[1:(window*nfac),1:(window*nfac)]=E
        #           VL[1:(window*nfac),(window*nfac+1):(window*nfac+window^2)]=kronecker(diag(1,window),U_inv%*%
        #                                                                                   t(factor.supp[,1:nfac])/window)%*%Ue
        # 
        #           #KK=commutation.matrix((nfac*window),(nfac*window))
        #           VL[1:(window*nfac),(window*nfac+window^2+1):(2*window*nfac+window^2)]=
        #             kronecker(sigma2*diag(1,window),diag(1,nfac)) +
        #             kronecker( factor.supp[,1:nfac]%*%U, sigma2*U_inv%*%t(factor.supp[,1:nfac])/window )%*%t(KK)
        #           VL[1:(window*nfac),(2*window*nfac+window^2+1):(2*window*nfac+window^2+window)]=
        #             kronecker(sigma2*diag(1,window),U_inv%*%mulambda) +
        #                           kronecker(factor.supp[,1:nfac]%*%mulambda,sigma2*U_inv%*%t(factor.supp[,1:nfac])/window )
        #           VL[(window*nfac+1):(window*nfac+window^2),1:(window*nfac)]=t(VL[1:(window*nfac),(window*nfac+1):(window*nfac+window^2)])
        #           VL[(window*nfac+1):(window*nfac+window^2),(1+window*nfac):(window^2+window*nfac)]=Ue
        #           VL[(window*nfac+window^2+1):(2*window*nfac+window^2),1:(window*nfac)]=
        #             t(VL[1:(window*nfac),(window*nfac+window^2+1):(2*window*nfac+window^2)])
        #           VL[(window*nfac+window^2+1):(2*window*nfac+window^2),(window*nfac+window^2+1):(2*window*nfac+window^2)]=
        #           kronecker(sigma2*diag(1,window),U)
        #           VL[(window*nfac+window^2+1):(2*window*nfac+window^2),(2*window*nfac+window^2+1):(2*window*nfac+window^2+window)]=
        #           kronecker(sigma2*diag(1,window),mulambda)
        #           VL[(2*window*nfac+window^2+1):(2*window*nfac+window^2+window),1:(window*nfac)]=t( VL[1:(window*nfac),
        #                                                                 (2*window*nfac+window^2+1):(2*window*nfac+window^2+window)])
        #           VL[(2*window*nfac+window^2+1):(2*window*nfac+window^2+window),(window*nfac+window^2+1):(2*window*nfac+window^2)]=
        #           t( VL[(window*nfac+window^2+1):(2*window*nfac+window^2),(2*window*nfac+window^2+1):(2*window*nfac+window^2+window)])
        #           VL[(2*window*nfac+window^2+1):(2*window*nfac+window^2+window),(2*window*nfac+window^2+1):(2*window*nfac+window^2+window)]=
        #           sigma2*diag(1,window)
        #     VV=(DeltaI+DeltaII)%*%VL%*%t(DeltaI+DeltaII)
        # 
        # 
        # 
        # 
        #     DeltaSR=     (1/window)*matrix(1,1,window)/as.numeric(sqrt(t(rrr)%*%M%*%rrr/window))-
        #     as.numeric( mean(rrr) )/(as.numeric((t(rrr)%*%M%*%rrr/window)^1.5))%*%t(rrr)%*%M/window
        #     p=0.05
        #     zp=qnorm(p)
        #     DeltaVaR=(1/window)*matrix(1,1,window) -zp*as.numeric((t(rrr)%*%M%*%rrr/window)^(-0.5))*t(rrr)%*%M/window
        #     DeltaES=(1/window)*matrix(1,1,window) -(dnorm(zp)/p)*as.numeric((t(rrr)%*%M%*%rrr/window)^(-0.5))*t(rrr)%*%M/window
        # 
        #     se_SR[j]=  DeltaSR%*%VV%*%t(DeltaSR)/N
        #     se_VaR[j]=  DeltaVaR%*%VV%*%t(DeltaVaR)/N
        #     se_ES[j]=  DeltaES%*%VV%*%t(DeltaES)/N
        # 
        # 
        #     VaR_outv_is[j]=apply(rrr, 2, mean)+zp*apply(rrr, 2, sd)
        #     ES_outv_is[j]=apply(rrr, 2, mean)-(dnorm(zp)/p)*apply(rrr, 2, sd)
        # 
        #     CI_SR_plus[j]=SR_outv_is[j]+1.96*sqrt(se_SR)
        #     CI_SR_minus[j]=SR_outv_is[j]-1.96*sqrt(se_SR)
        # 
        #       CI_VaR_plus[j]= VaR_outv_is[j]+1.96*sqrt(se_VaR)
        #       CI_VaR_minus[j]= VaR_outv_is[j]-1.96*sqrt(se_VaR)
        # 
        #       CI_ES_plus[j]=ES_outv_is[j]+1.96*sqrt(se_ES)
        #       CI_ES_minus[j]=ES_outv_is[j]-1.96*sqrt(se_ES)
        # 
        # 
        #       # QQ
        #       # list(outt,outv,outtu,outvu,outv_roll,outvu_roll,SR_outt_is,SR_outv_is,SR_outtu_is,#9
        #       #      outtFF,outvFF,outtuFF,outvuFF,outv_rollFF,outvu_rollFF,SR_outt_isFF,SR_outv_isFF,SR_outtu_isFF,#18
        #       #      CI_SR_plus2,CI_SR_minus2,CI_VaR_plus2,CI_VaR_minus2,CI_ES_plus2,CI_ES_minus2,#24
        #       #      VaR_outt_is,ES_outt_is,
        #       #      VaR_outv_is2,ES_outv_is2,
        #       #      VaR_outtu_is,ES_outtu_is,#30
        #       #      VaR_outt_isFF,ES_outt_isFF,
        #       #      VaR_outv_isFF,ES_outv_isFF, #34
        #       #      VaR_outtu_isFF,ES_outtu_isFF,SR_outv_is2,CI_SR_plus3,CI_SR_minus3,stat,statu,statew,statewu,pp,ppu,#45
        #       #      outvu_roll2,outvu2,ES_outtu_is2,VaR_outtu_is2,outtu2, outvu_roll3,outvu3,ES_outtu_is3,VaR_outtu_is3,outtu3,
        #       #      outvu_roll4,outvu4,ES_outtu_is4,VaR_outtu_is4,outtu4)
        #       # QQ
        #       #
        # 
        # 
        # 
        #       # using  formula efficient pf!!
        # 
        #       gam=sqrt(t(risk_premia.supp[1:nfac])%*%solve(omega)%*%risk_premia.supp[1:nfac])
        # 
        # 
        #       DeltaSR2=(1/as.numeric(gam))*(  kronecker(matrix(1,1,window)/window,t(risk_premia.supp[1:nfac])%*%solve(omega))-
        #                       kronecker(t(risk_premia.supp[1:nfac])%*%solve(omega)%*%t(factor.supp[,1:nfac])%*%M/window,t(risk_premia.supp[1:nfac])%*%solve(omega))  )
        # 
        #       DeltaVaR2=tar_vol*DeltaSR2
        # 
        # 
        # 
        #       se_SR2=  DeltaSR2%*%E%*%t(DeltaSR2)
        #       se_VaR2=  DeltaVaR2%*%E%*%t(DeltaVaR2)
        #       se_ES2=  DeltaVaR2%*%E%*%t(DeltaVaR2)
        # 
        #       SR_outv_is2[j]=gam
        #       VaR_outv_is2[j]=gam*tar_vol  +zp*tar_vol
        #       ES_outv_is2[j]=gam*tar_vol     -(dnorm(zp)/p)*tar_vol
        # 
        #       CI_SR_plus2[j]=gam+1.96*sqrt(se_SR2/N)#SR_outv_is[j]+1.96*sqrt(se_SR2/N)
        #       CI_SR_minus2[j]=gam-1.96*sqrt(se_SR2/N)#SR_outv_is[j]-1.96*sqrt(se_SR2/N)
        # 
        #       CI_SR_plus3[j]=SR_outv_is[j]+1.96*sqrt(12)*sqrt(se_SR2/N)
        #       CI_SR_minus3[j]=SR_outv_is[j]-1.96*sqrt(12)*sqrt(se_SR2/N)
        # 
        # 
        # 
        #       CI_VaR_plus2[j]= VaR_outv_is2[j]+1.96*sqrt(se_VaR2/N)
        #       CI_VaR_minus2[j]= VaR_outv_is2[j]-1.96*sqrt(se_VaR2/N)
        # 
        #       CI_ES_plus2[j]=ES_outv_is2[j]+1.96*sqrt(se_ES2/N)
        #       CI_ES_minus2[j]=ES_outv_is2[j]-1.96*sqrt(se_ES2/N)
        # 
      }
      
      
      #CI_SR_plus,CI_SR_minus,CI_VaR_plus,CI_VaR_minus,CI_ES_plus,CI_ES_minus,
      
      
      
      outv_roll[j] = sum(w22[!redistribute]*X.next[!redistribute])
      
      
      
      # 
      # if (l >= 2) 
      # { comandi }
      #i comandi nelle parentesi graffe saranno eseguiti solo se l >= 2.
      
      #QQQ  uncon
      
      nfacu = ku[j]
      ## massimo
      # omegau = diag(1, nfacu) - gammak[1:nfacu] %*% t(gammak[1:nfacu])
      # sigma2u = windowu/(windowu - nfacu)*valuefu[nfacu]
      # 
      # Su = 1/sigma2u*diag(1, N)
      # w2u = solve(solve(omegau) + t(lambdau[-delete,1:nfacu]) %*% S %*% lambdau[-delete,1:nfacu])
      # tempu = lambdau[-delete,1:nfacu]
      # tempu = Su %*% tempu
      # w1u = Su - tempu %*% w2u %*% t(tempu)
      # 
      # # per il peso devo premoltiplicare per l'inverse della covariance matrix dei returns
      # # che e' uguale a (S^-1 + AOmegaA'). La inverto con sherman-morrison/woodbury
      # wu = w1u %*% lambdau[-delete,1:nfacu] %*% gammak[1:nfacu]
      # 
      # wu[redistribute] = 0
      # wu = wu/sum(wu)
      #   
      # outu[j] = sum(wu[!redistribute]*X.next[!redistribute])
      # # massimo
      
      # UU
      # old_delete=delete
      # old_red=redistribute
      # delete=!balancedN
      # redistribute=!balancedN
      # 
      
      # case 1 expanding wind
      {
        
        selectu = balancedN
        # apply(data, 2, function(x){sum(is.na(x))})
        #selectu = (selectu == 0)
        data_balanced = data[1:(i+window-1),selectu]
        data_balanced=data_balanced-(rf[1:(i+window-1)]-1)%*%matrix(1,1,ncol(data_balanced))
        
        data_balanced_oos = data[index_oos,selectu]
        data_balanced_oos=data_balanced_oos - (rf[index_oos]-1)%*%matrix(1,1,ncol(data_balanced_oos))
        
        
        kmax=10
        XX=data_balanced#data[,qqq]
        TTT=nrow(XX)
        FPCAu = sqrt(TTT) * as.matrix(eigen(XX %*% t(XX))$vectors[,1:kmax])
        lambdauPCA = t((1/TTT) * t(FPCAu) %*% XX)
        valuefuPCA=matrix(0,10,1)
        kk=1;
        for (kk in 1:10)
        {valuefuPCA[kk]=(1/(ncol(XX)*nrow(XX)))*tr( (XX - FPCAu[,1:kk]%*%t(lambdauPCA[,1:kk]))%*%
                                                      t( XX - FPCAu[,1:kk]%*%t(lambdauPCA[,1:kk])   )     )
        }
        gammakPCA=apply(FPCAu,2,mean)
        omegakPCA=cov(FPCAu)
        windowu=TTT
        #lambdau=lambdaPCAu
        rm(XX,data_balanced)
        
        
        
        gamu=sqrt(t(gammakPCA[1:nfacu])%*%solve(omegakPCA[1:nfacu,1:nfacu])%*%gammakPCA[1:nfacu])
        SR_outvu_is[j]=gamu
        VaR_outvu_is[j]=gamu*tar_vol  +zp*tar_vol
        ES_outvu_is[j]=gamu*tar_vol     -(dnorm(zp)/p)*tar_vol
        
        
        
        
        #
        
        omegau = diag(1, nfacu) - gammakPCA[1:nfacu] %*% t(gammakPCA[1:nfacu])
        sigma2u = windowu/(windowu - nfacu)*valuefuPCA[nfacu]
        sigmalambdau=(t(lambdauPCA[,1:nfacu]) %*% lambdauPCA[,1:nfacu]) - (N/windowu)*sigma2u*diag(1,nfacu)
        sigmalambdau1=(t(lambdauPCA[,1:nfacu]) %*% lambdauPCA[,1:nfacu])
        w2u = solve(sigma2u*solve(omegau) + sigmalambdau)
        aaau=t(gammakPCA[1:nfacu])%*%solve(omegau)%*%w2u%*%sigmalambdau%*%w2u%*%solve(omegau)%*%gammakPCA[1:nfacu]
        
        if(aaau<=0)      { sigmalambdau=sigmalambdau1}
        
        w2u=matrix(NA,nrow=nfacu, ncol=nfacu)
        w2u = solve(sigma2u*solve(omegau) + sigmalambdau)
        wu =  lambdauPCA[,1:nfacu] %*% w2u %*% solve(omegau)%*%gammakPCA[1:nfacu]
        #wu[redistribute] = 0
        wu_tanu = wu/sum(wu)
        outtu[j] = sum(wu_tanu*X.next2[balancedN])
        
        
        rrr=matrix(NA,nrow=window,ncol=1)
        # here change from old to new red!
        rrr= X_in2[,balancedN]%*%wu_tanu#  - (N/window)*sigma2*factor.supp
        SR_outtu_is[j]=sqrt(12)*apply(rrr, 2, mean)/apply(rrr, 2, sd)
        
        rrr_oos=matrix(NA,nrow=window,ncol=1)
        # here change from old to new red!
        rrr_oos= data_balanced_oos%*%wu_tanu#  - (N/window)*sigma2*factor.supp
        SR_outtu_oos[j]=sqrt(12)*apply(rrr_oos, 2, mean)/apply(rrr_oos, 2, sd)
        
        
        
        
        p=0.05
        zp=qnorm(p)
        
        VaR_outtu_is[j]=apply(rrr, 2, mean)+zp*apply(rrr, 2, sd)
        ES_outtu_is[j]=apply(rrr, 2, mean)-(dnorm(zp)/p)*apply(rrr, 2, sd)
        
        
        VaR_outtu_oos[j]=apply(rrr_oos, 2, mean)+zp*apply(rrr_oos, 2, sd)
        ES_outtu_oos[j]=apply(rrr_oos, 2, mean)-(dnorm(zp)/p)*apply(rrr_oos, 2, sd)
        
        
        
        
        aaau=t(gammakPCA[1:nfacu])%*%solve(omegau)%*%w2u%*%sigmalambdau%*%w2u%*%solve(omegau)%*%gammakPCA[1:nfacu]
        volpu=sqrt(    t(gammakPCA[1:nfacu])%*%solve(omegau)%*%w2u%*%sigmalambdau%*%w2u%*%solve(omegau)%*%gammakPCA[1:nfacu])
        
        
        tar_vol=0.05/sqrt(12)
        wu_vol =  (tar_vol)*wu%*%solve(volpu)
        outvu[j] = sum(wu_vol*X.next2[balancedN])
        
        outvu_roll[j] = sum(wu*X.next2[balancedN])
      }
      
      
      
      #   case  constant parameter estimated over full sample (insample)
      
      {
        
        selectu = balancedN
        # apply(data, 2, function(x){sum(is.na(x))})
        #selectu = (selectu == 0)
        data_balanced = data[,selectu]
        data_balanced=data_balanced-(rf-1)%*%matrix(1,1,ncol(data_balanced))
        
        data_balanced_oos = data[index_oos,selectu]
        data_balanced_oos=data_balanced_oos - (rf[index_oos]-1)%*%matrix(1,1,ncol(data_balanced_oos))
        
        
        kmax=10
        XX=data_balanced#data[,qqq]
        TTT=nrow(XX)
        FPCAu = sqrt(TTT) * as.matrix(eigen(XX %*% t(XX))$vectors[,1:kmax])
        lambdauPCA = t((1/TTT) * t(FPCAu) %*% XX)
        valuefuPCA=matrix(0,10,1)
        kk=1;
        for (kk in 1:10)
        {valuefuPCA[kk]=(1/(ncol(XX)*nrow(XX)))*tr( (XX - FPCAu[,1:kk]%*%t(lambdauPCA[,1:kk]))%*%
                                                      t( XX - FPCAu[,1:kk]%*%t(lambdauPCA[,1:kk])   )     )
        }
        gammakPCA=apply(FPCAu,2,mean)
        omegakPCA=cov(FPCAu)
        windowu=TTT
        #lambdau=lambdaPCAu
        rm(XX,data_balanced)
        
        
        
        gamu=sqrt(t(gammakPCA[1:nfacu])%*%solve(omegakPCA[1:nfacu,1:nfacu])%*%gammakPCA[1:nfacu])
        SR_outvu_is2[j]=gamu
        VaR_outvu_is2[j]=gamu*tar_vol  +zp*tar_vol
        ES_outvu_is2[j]=gamu*tar_vol     -(dnorm(zp)/p)*tar_vol
        
        
        
        
        #
        
        omegau = diag(1, nfacu) - gammakPCA[1:nfacu] %*% t(gammakPCA[1:nfacu])
        sigma2u = windowu/(windowu - nfacu)*valuefuPCA[nfacu]
        sigmalambdau=(t(lambdauPCA[,1:nfacu]) %*% lambdauPCA[,1:nfacu]) - (N/windowu)*sigma2u*diag(1,nfacu)
        sigmalambdau1=(t(lambdauPCA[,1:nfacu]) %*% lambdauPCA[,1:nfacu])
        w2u = solve(sigma2u*solve(omegau) + sigmalambdau)
        aaau=t(gammakPCA[1:nfacu])%*%solve(omegau)%*%w2u%*%sigmalambdau%*%w2u%*%solve(omegau)%*%gammakPCA[1:nfacu]
        
        if(aaau<=0)      { sigmalambdau=sigmalambdau1}
        
        w2u=matrix(NA,nrow=nfacu, ncol=nfacu)
        w2u = solve(sigma2u*solve(omegau) + sigmalambdau)
        wu =  lambdauPCA[,1:nfacu] %*% w2u %*% solve(omegau)%*%gammakPCA[1:nfacu]
        #wu[redistribute] = 0
        wu_tanu = wu/sum(wu)
        outtu2[j] = sum(wu_tanu*X.next2[balancedN])
        
        
        rrr=matrix(NA,nrow=window,ncol=1)
        # here change from old to new red!
        rrr= X_in2[,balancedN]%*%wu_tanu#  - (N/window)*sigma2*factor.supp
        SR_outtu_is2[j]=sqrt(12)*apply(rrr, 2, mean)/apply(rrr, 2, sd)
        
        rrr_oos=matrix(NA,nrow=window,ncol=1)
        # here change from old to new red!
        rrr_oos= data_balanced_oos%*%wu_tanu#  - (N/window)*sigma2*factor.supp
        SR_outtu_oos2[j]=sqrt(12)*apply(rrr_oos, 2, mean)/apply(rrr_oos, 2, sd)
        
        
        
        
        p=0.05
        zp=qnorm(p)
        
        VaR_outtu_is2[j]=apply(rrr, 2, mean)+zp*apply(rrr, 2, sd)
        ES_outtu_is2[j]=apply(rrr, 2, mean)-(dnorm(zp)/p)*apply(rrr, 2, sd)
        
        
        VaR_outtu_oos2[j]=apply(rrr_oos, 2, mean)+zp*apply(rrr_oos, 2, sd)
        ES_outtu_oos2[j]=apply(rrr_oos, 2, mean)-(dnorm(zp)/p)*apply(rrr_oos, 2, sd)
        
        
        
        
        aaau=t(gammakPCA[1:nfacu])%*%solve(omegau)%*%w2u%*%sigmalambdau%*%w2u%*%solve(omegau)%*%gammakPCA[1:nfacu]
        volpu=sqrt(    t(gammakPCA[1:nfacu])%*%solve(omegau)%*%w2u%*%sigmalambdau%*%w2u%*%solve(omegau)%*%gammakPCA[1:nfacu])
        
        
        tar_vol=0.05/sqrt(12)
        wu_vol =  (tar_vol)*wu%*%solve(volpu)
        outvu2[j] = sum(wu_vol*X.next2[balancedN])
        
        outvu_roll2[j] = sum(wu*X.next2[balancedN])
      }
      
      
      
      
      # case 2 fixed window
      
      {
        #   #rm(lambdauPCA,data_balanced)
        #       www1=round(nrow(data)/4)
        #       data_balanced = data[1:www1,selectu]
        #       data_balanced=data_balanced-(rf[1:www1]-1)%*%matrix(1,1,ncol(data_balanced))
        # 
        # 
        #       kmax=10
        #       XX=data_balanced#data[,qqq]
        #       TTT=nrow(XX)
        #       FPCAu = sqrt(TTT) * as.matrix(eigen(XX %*% t(XX))$vectors[,1:kmax])
        #       lambdauPCA = t((1/TTT) * t(FPCAu) %*% XX)
        #       valuefuPCA=matrix(0,10,1)
        #       kk=1;
        #       for (kk in 1:10)
        #       {valuefuPCA[kk]=(1/(ncol(XX)*nrow(XX)))*tr( (XX - FPCAu[,1:kk]%*%t(lambdauPCA[,1:kk]))%*%
        #                                                     t( XX - FPCAu[,1:kk]%*%t(lambdauPCA[,1:kk])   )     )
        #       }
        #       gammakPCA=apply(FPCAu,2,mean)
        #       windowu=TTT
        #       #lambdau=lambdaPCAu
        #       rm(XX,data_balanced)
        # 
        # 
        #       #
        # 
        #       omegau = diag(1, nfacu) - gammakPCA[1:nfacu] %*% t(gammakPCA[1:nfacu])
        #       sigma2u = windowu/(windowu - nfacu)*valuefuPCA[nfacu]
        #       sigmalambdau=(t(lambdauPCA[,1:nfacu]) %*% lambdauPCA[,1:nfacu]) - (N/windowu)*sigma2u*diag(1,nfacu)
        #       sigmalambdau1=(t(lambdauPCA[,1:nfacu]) %*% lambdauPCA[,1:nfacu])
        #       w2u = solve(sigma2u*solve(omegau) + sigmalambdau)
        #       aaau=t(gammakPCA[1:nfacu])%*%solve(omegau)%*%w2u%*%sigmalambdau%*%w2u%*%solve(omegau)%*%gammakPCA[1:nfacu]
        # 
        #       if(aaau<=0)      { sigmalambdau=sigmalambdau1}
        # 
        #       w2u=matrix(NA,nrow=nfacu, ncol=nfacu)
        #       w2u = solve(sigma2u*solve(omegau) + sigmalambdau)
        #       wu =  lambdauPCA[,1:nfacu] %*% w2u %*% solve(omegau)%*%gammakPCA[1:nfacu]
        #       #wu[redistribute] = 0
        #       wu_tanu = wu/sum(wu)
        #       outtu2[j] = sum(wu_tanu*X.next2[balancedN])
        # 
        # 
        #       rrr=matrix(NA,nrow=window,ncol=1)
        #       # here change from old to new red!
        #       rrr= X_in2[,balancedN]%*%wu_tanu#  - (N/window)*sigma2*factor.supp
        #       SR_outtu_is2[j]=sqrt(12)*apply(rrr, 2, mean)/apply(rrr, 2, sd)
        # 
        # 
        #       p=0.05
        #       zp=qnorm(p)
        # 
        #       VaR_outtu_is2[j]=apply(rrr, 2, mean)+zp*apply(rrr, 2, sd)
        #       ES_outtu_is2[j]=apply(rrr, 2, mean)-(dnorm(zp)/p)*apply(rrr, 2, sd)
        # 
        # 
        # 
        # 
        # 
        #       aaau=t(gammakPCA[1:nfacu])%*%solve(omegau)%*%w2u%*%sigmalambdau%*%w2u%*%solve(omegau)%*%gammakPCA[1:nfacu]
        #       volpu=sqrt(    t(gammakPCA[1:nfacu])%*%solve(omegau)%*%w2u%*%sigmalambdau%*%w2u%*%solve(omegau)%*%gammakPCA[1:nfacu])
        # 
        # 
        #       tar_vol=0.05/sqrt(12)
        #       wu_vol =  (tar_vol)*wu%*%solve(volpu)
        #       outvu2[j] = sum(wu_vol*X.next2[balancedN])
        # 
        #       outvu_roll2[j] = sum(wu*X.next2[balancedN])
        # 
      }
      
      
      
      
      # case 1  unbal expanding wind
      {
        
        # # selectu = apply(data, 2, function(x){sum(is.na(x))})
        # # selectu = (selectu == 0)
        # # # data_balanced = data[1:(i+window-1),selectu]
        # # data_balanced=data_balanced-(rf[1:(i+window-1)]-1)%*%matrix(1,1,ncol(data_balanced))
        # # windowu=nrow(data_balanced)
        # 
        # # kmax=10
        # # XX=data_balanced#data[,qqq]
        # # TTT=nrow(XX)
        # # FPCAu = sqrt(TTT) * as.matrix(eigen(XX %*% t(XX))$vectors[,1:kmax])
        # # lambdaPCAu = t((1/TTT) * t(FPCAu) %*% XX)
        # # valuefuPCA=matrix(0,10,1)
        # # kk=1;
        # # for (kk in 1:10)
        # # {valuefuPCA[kk]=(1/(ncol(XX)*nrow(XX)))*tr( (XX - FPCAu[,1:kk]%*%t(lambdaPCAu[,1:kk]))%*%
        # #                                                           t( XX - FPCAu[,1:kk]%*%t(lambdaPCAu[,1:kk])   )     )
        # # }
        # # gammak=apply(FPCAu,2,mean)
        # # windowu=TTT
        # # lambdau=lambdaPCAu
        # # rm(XX,data_balanced)
        # 
        # 
        # #
        # 
        # omegau = diag(1, nfacu) - gammak[1:nfacu] %*% t(gammak[1:nfacu])
        # sigma2u = windowu/(windowu - nfacu)*valuefu[nfacu]
        # sigmalambdau=(t(lambdau[!redistribute,1:nfacu]) %*% lambdau[!redistribute,1:nfacu]) - (N/windowu)*sigma2u*diag(1,nfacu)
        #     sigmalambdau1=(t(lambdau[!redistribute,1:nfacu]) %*% lambdau[!redistribute,1:nfacu])
        #     w2u = solve(sigma2u*solve(omegau) + sigmalambdau)
        #     aaau=t(gammak[1:nfacu])%*%solve(omegau)%*%w2u%*%sigmalambdau%*%w2u%*%solve(omegau)%*%gammak[1:nfacu]
        # 
        #     if(aaau<=0)      { sigmalambdau=sigmalambdau1}
        # 
        #     w2u=matrix(NA,nrow=nfacu, ncol=nfacu)
        #          w2u = solve(sigma2u*solve(omegau) + sigmalambdau)
        # wu =  lambdau[!redistribute,1:nfacu] %*% w2u %*% solve(omegau)%*%gammak[1:nfacu]
        # wu[redistribute] = 0
        # wu_tanu = wu/sum(wu)
        # outtu3[j] = sum(wu_tanu[!redistribute]*X.next[!redistribute])
        # 
        # 
        # rrr=matrix(NA,nrow=window,ncol=1)
        # # here change from old to new red!
        # rrr= X_in2[,balancedN]%*%wu_tanu#  - (N/window)*sigma2*factor.supp
        # SR_outtu_is3[j]=sqrt(12)*apply(rrr, 2, mean)/apply(rrr, 2, sd)
        # 
        # 
        # p=0.05
        # zp=qnorm(p)
        # 
        # VaR_outtu_is3[j]=apply(rrr, 2, mean)+zp*apply(rrr, 2, sd)
        # ES_outtu_is3[j]=apply(rrr, 2, mean)-(dnorm(zp)/p)*apply(rrr, 2, sd)
        # 
        # 
        # 
        # 
        # 
        # aaau=t(gammak[1:nfacu])%*%solve(omegau)%*%w2u%*%sigmalambdau%*%w2u%*%solve(omegau)%*%gammak[1:nfacu]
        # volpu=sqrt(    t(gammak[1:nfacu])%*%solve(omegau)%*%w2u%*%sigmalambdau%*%w2u%*%solve(omegau)%*%gammak[1:nfacu])
        # 
        # 
        #     tar_vol=0.05/sqrt(12)
        # wu_vol =  (tar_vol)*wu%*%solve(volpu)
        # outvu3[j] = sum(wu_vol*X.next2[balancedN])
        # 
        # outvu_roll3[j] = sum(wu*X.next2[balancedN])
      }   
      
      
      # case 2 - unbal fixed window
      
      {
        
        # # selectu = apply(data, 2, function(x){sum(is.na(x))})
        # # selectu = (selectu == 0)
        # # 
        # # data_balanced = data[1:100,selectu]
        # # data_balanced=data_balanced-(rf[1:100]-1)%*%matrix(1,1,ncol(data_balanced)) 
        # # 
        # # kmax=10
        # # XX=data_balanced#data[,qqq]
        # # TTT=nrow(XX)
        # # FPCAu = sqrt(TTT) * as.matrix(eigen(XX %*% t(XX))$vectors[,1:kmax])
        # # lambdaPCAu = t((1/TTT) * t(FPCAu) %*% XX)
        # # valuefuPCA=matrix(0,10,1)
        # # kk=1;
        # # for (kk in 1:10)
        # # {valuefuPCA[kk]=(1/(ncol(XX)*nrow(XX)))*tr( (XX - FPCAu[,1:kk]%*%t(lambdaPCAu[,1:kk]))%*%
        # #                                               t( XX - FPCAu[,1:kk]%*%t(lambdaPCAu[,1:kk])   )     )
        # # }
        # # gammak=apply(FPCAu,2,mean)
        # # windowu=nrow(data_balanced)
        # # lambdau=lambdaPCAu
        # # rm(XX,data_balanced)
        # #   
        # 
        # #
        # 
        # omegau = diag(1, nfacu) - gammak[1:nfacu] %*% t(gammak[1:nfacu])
        # sigma2u = windowu/(windowu - nfacu)*valuefu[nfacu]
        # sigmalambdau=(t(lambdau[,1:nfacu]) %*% lambdau[,1:nfacu]) - (N/window)*sigma2u*diag(1,nfacu)
        # sigmalambdau1=(t(lambdau[,1:nfacu]) %*% lambdau[,1:nfacu])
        # w2u = solve(sigma2u*solve(omegau) + sigmalambdau)
        # aaau=t(gammak[1:nfacu])%*%solve(omegau)%*%w2u%*%sigmalambdau%*%w2u%*%solve(omegau)%*%gammak[1:nfacu]
        # 
        # if(aaau<=0)      { sigmalambdau=sigmalambdau1}
        # 
        # w2u=matrix(NA,nrow=nfacu, ncol=nfacu)
        # w2u = solve(sigma2u*solve(omegau) + sigmalambdau)
        # wu =  lambdau[,1:nfacu] %*% w2u %*% solve(omegau)%*%gammak[1:nfacu]
        # #wu[redistribute] = 0
        # wu_tanu = wu/sum(wu)
        # outtu4[j] = sum(wu_tanu*X.next2[balancedN])
        # 
        # 
        # rrr=matrix(NA,nrow=window,ncol=1)
        # # here change from old to new red!
        # rrr= X_in2[,balancedN]%*%wu_tanu#  - (N/window)*sigma2*factor.supp
        # SR_outtu_is4[j]=sqrt(12)*apply(rrr, 2, mean)/apply(rrr, 2, sd)
        # 
        # 
        # p=0.05
        # zp=qnorm(p)
        # 
        # VaR_outtu_is4[j]=apply(rrr, 2, mean)+zp*apply(rrr, 2, sd)
        # ES_outtu_is4[j]=apply(rrr, 2, mean)-(dnorm(zp)/p)*apply(rrr, 2, sd)
        # 
        # 
        # 
        # 
        # 
        # aaau=t(gammak[1:nfacu])%*%solve(omegau)%*%w2u%*%sigmalambdau%*%w2u%*%solve(omegau)%*%gammak[1:nfacu]
        # volpu=sqrt(    t(gammak[1:nfacu])%*%solve(omegau)%*%w2u%*%sigmalambdau%*%w2u%*%solve(omegau)%*%gammak[1:nfacu])
        # 
        # 
        # tar_vol=0.05/sqrt(12)
        # wu_vol =  (tar_vol)*wu%*%solve(volpu)
        # outvu4[j] = sum(wu_vol*X.next2[balancedN])
        # 
        # outvu_roll4[j] = sum(wu*X.next2[balancedN])
        # 
      }
      
      
      
      
      # delete= old_delete
      # redistribute=old_red
      #
      #
      #
      # p=0.05
      # zp=qnorm(p)
      #
      # VaR_outvu_is[j]=apply(rrr, 2, mean)+zp*apply(rrr, 2, sd)
      # ES_outvu_is[j]=apply(rrr, 2, mean)-(dnorm(zp)/p)*apply(rrr, 2, sd)
      #
      
      
      # end uncond
      
      
    }
    
    stat=cbind(max(X_in[,!redistribute]),min(X_in[,!redistribute]),mean(X_in[,!redistribute]),sd(X_in[,!redistribute]))
    statu=cbind(max(X_in2[,balancedN]),min(X_in2[,balancedN]),mean(X_in2[,balancedN]),sd(X_in2[,balancedN]))
    
    ew=apply(X_in[,!redistribute],1,mean) 
    statew=cbind(max(ew),min(ew),mean(ew),sd(ew))
    ewu=apply(X_in2[,balancedN],1,mean) 
    statewu=cbind(max(ewu),min(ewu),mean(ewu),sd(ewu))
    
    
    ppu=mean(X.next2[balancedN])
    pp=mean(X.next[!redistribute])
    
    
    
    
    
    
    
    
    
    # old_delete=delete
    # old_red=redistribute
    
    
    w = 1/(N - sum(redistribute))
    outt[length(outt)] = w*sum(X.next[!redistribute])
    
    w = 1/(N - sum(redistribute))
    outv[length(outv)] = w*sum(X.next[!redistribute])
    
    w = 1/(N - sum(redistribute))
    outvu[length(outvu)] = w*sum(X.next[!redistribute])
    
    w = 1/(N - sum(redistribute))
    outtu[length(outtu)] = w*sum(X.next[!redistribute])
    
    w = 1/(N - sum(redistribute))
    outv_roll[length(outv_roll)] = w*sum(X.next[!redistribute])
    
    w = 1/(N - sum(redistribute))
    outvu_roll[length(outvu_roll)] = w*sum(X.next[!redistribute])
    
    
    rrr=matrix(NA,nrow=window,ncol=1)
    
    #rrr= X_in[,!redistribute]%*%w[!redistribute]
    Nnew=ncol(X_in[,!redistribute])
    rrr=  X_in[,!redistribute]%*%matrix(1,nrow=Nnew,ncol=1)/Nnew
    
    SR_outt_is[length(SR_outt_is)]=sqrt(12)*apply(rrr, 2, mean)/apply(rrr, 2, sd)
    SR_outtu_is[length(SR_outtu_is)]=sqrt(12)*apply(rrr, 2, mean)/apply(rrr, 2, sd)
    SR_outv_is[length(SR_outt_is)]=sqrt(12)*apply(rrr, 2, mean)/apply(rrr, 2, sd)
    SR_outtu_is[length(SR_outtu_is)]=sqrt(12)*apply(rrr, 2, mean)/apply(rrr, 2, sd)
    
    VaR_outt_is[length(SR_outt_is)]=apply(rrr, 2, mean)+zp*apply(rrr, 2, sd)
    ES_outt_is[length(SR_outt_is)]=apply(rrr, 2, mean)-(dnorm(zp)/p)*apply(rrr, 2, sd)
    VaR_outv_is[length(SR_outt_is)]=apply(rrr, 2, mean)+zp*apply(rrr, 2, sd)
    ES_outv_is[length(SR_outt_is)]=apply(rrr, 2, mean)-(dnorm(zp)/p)*apply(rrr, 2, sd)
    VaR_outtu_is[length(SR_outt_is)]=apply(rrr, 2, mean)+zp*apply(rrr, 2, sd)
    ES_outtu_is[length(SR_outt_is)]=apply(rrr, 2, mean)-(dnorm(zp)/p)*apply(rrr, 2, sd)
    
    
    
    
    #FF
    #QQQ
    
    
    
    
    factorsFF = FF[index,]
    factorsFF.in=factorsFF[1:window,]
    #factor.suppFF = ts.factors[,,i]
    
    
    MM=diag(1,nrow=window)-matrix(1,window,1)%*%matrix(1,1,window)/window
    lambda.suppFF =solve(t(factorsFF.in)%*%MM%*%factorsFF.in)%*%t(factorsFF.in)%*%MM%*%X[1:window,] 
    eFF=X[1:window,]-factorsFF.in%*%lambda.suppFF
    sigma2FF=tr(eFF%*%t(eFF))/(N*(window-5-1))
    
    
    
    risk_premia.suppFF = apply(factorsFF.in, 2, mean)
    #valuef.supp = ts.valuef[, i]
    
    #k = 5# c(ts.nfactorhat[i], k0)
    #ku = c(nfactorhatu, k0)
    #X.next = data[window + i,]
    # lead=window+1
    # X.next=matrix(NA,nrow=window+1,ncol=N)
    # X.next = matrix(X[lead:lead,],nrow=1,ncol=N)
    # X_in=X[1:window,]
    #  QQQQ
    # if (!is.na(ts.deletestocks[[1]][1]))
    #X.next =matrix(X.next[-delete],nrow=1,ncol=N)
    #X.next =X.next[-delete]
    redistribute = is.na(X.next)
    
    # if (!is.na(ts.deletestocks[[1]][1]))    
    #X_in=X_in[,-delete]
    # #redistribute_is = is.na(X_in)
    N_in=ncol(X_in)
    N_next=ncol(X.next)
    
    #QQQ
    
    outtFF = rep(NA, 2)
    outvFF = rep(NA,  2)
    outtuFF = rep(NA, 2)
    outvuFF = rep(NA, 2)
    outvu_rollFF = rep(NA,  1)
    outv_rollFF = rep(NA, 1)
    
    
    SR_outt_isFF = rep(NA,  1)
    SR_outv_isFF = rep(NA,  1)
    SR_outtu_isFF = rep(NA, 1)
    SR_outvu_isFF = rep(NA, 1)
    SR_outvu_roll_isFF = rep(NA,  1)
    SR_outv_roll_isFF = rep(NA,  1)
    
    SR_outt_oosFF = rep(NA,  1)
    VaR_outt_oosFF = rep(NA,  1)
    ES_outt_oosFF = rep(NA,  1)
    
    
    
    SR_outv_oosFF = rep(NA,  1)
    VaR_outv_oosFF = rep(NA,  1)
    ES_outv_oosFF = rep(NA,  1)
    
    
    
    #for (j in 1:length(k))
    
    j=1
    
    nfac = 5
    
    
    # #massimo way start
    # omega = diag(1, nfac) - risk_premia.supp[1:nfac] %*% t(risk_premia.supp[1:nfac])
    # sigma2 = window/(window - nfac)*valuef.supp[nfac]
    # 
    # S = 1/sigma2*diag(1, N)
    # w2 = solve(solve(omega) + t(lambda.supp[,1:nfac]) %*% S %*% lambda.supp[,1:nfac])
    # temp = lambda.supp[,1:nfac]
    # temp = S %*% temp
    # w1 = S - temp %*% w2 %*% t(temp)
    # 
    # # per il peso devo premoltiplicare per l'inverse della covariance matrix dei returns
    # # che e' uguale a (S^-1 + AOmegaA'). La inverto con sherman-morrison/woodbury
    # w = w1 %*% lambda.supp[,1:nfac] %*% risk_premia.supp[1:nfac]
    ## massimo way end
    
    omegaFF = cov(factorsFF)  #diag(1, nfac) - risk_premia.supp[1:nfac] %*% t(risk_premia.supp[1:nfac])
    #sigma2 = window/(window - nfac)*valuef.supp[nfac]
    # sigmalambda=(t(lambda.supp[,1:nfac]) %*% lambda.supp[,1:nfac]) - (N/window)*sigma2*diag(1,nfac)
    sigmalambdaFF=((lambda.suppFF) %*% t(lambda.suppFF))
    w2FF = solve(sigma2FF*solve(omegaFF) + sigmalambdaFF)
    
    # aaa=( t(risk_premia.suppFF)%*%solve(omegaFF)%*%w2FF%*%sigmalambdaFF%*%w2FF%*%solve(omegaFF)%*%risk_premia.suppFF)
    # 
    # 
    # if(aaa<=0)     { sigmalambdaFF=sigmalambdaFF}      
    # 
    # w2=matrix(NA,nrow=nfac, ncol=nfac)
    # 
    # w2 = solve(sigma2*solve(omega) + sigmalambda)
    wFF =  t(lambda.suppFF) %*% w2FF %*% solve(omegaFF)%*%risk_premia.suppFF
    wFF[redistribute] = 0
    w_tanFF = wFF/sum(wFF)
    outtFF[1] = sum(w_tanFF[!redistribute]*X.next[!redistribute])
    
    
    rrr=matrix(NA,nrow=window,ncol=1)
    rrr= X_in[,!redistribute]%*%w_tanFF[!redistribute]
    SR_outt_isFF[1]=sqrt(12)*apply(rrr, 2, mean)/apply(rrr, 2, sd)
    
    
    
    rrr_oos=matrix(NA,nrow=window,ncol=1)
    rrr_oos= X_oos[,!redistribute]%*%w_tanFF[!redistribute]
    SR_outt_oosFF[1]=sqrt(12)*apply(rrr_oos, 2, mean)/apply(rrr_oos, 2, sd)
    
    
    p=0.05
    zp=qnorm(p)
    
    VaR_outt_isFF[j]=apply(rrr, 2, mean)+zp*apply(rrr, 2, sd)
    ES_outt_isFF[j]=apply(rrr, 2, mean)-(dnorm(zp)/p)*apply(rrr, 2, sd)
    
    
    VaR_outt_oosFF[j]=apply(rrr_oos, 2, mean)+zp*apply(rrr_oos, 2, sd)
    ES_outt_oosFF[j]=apply(rrr_oos, 2, mean)-(dnorm(zp)/p)*apply(rrr_oos, 2, sd)
    
    
    volpFF=sqrt( t(risk_premia.suppFF)%*%solve(omegaFF)%*%w2FF%*%sigmalambdaFF%*%w2FF%*%solve(omegaFF)%*%risk_premia.suppFF)
    tar_vol=0.05/sqrt(12)
    w_volFF = (tar_vol)*wFF%*%solve(volpFF)
    outvFF[1] = sum(w_volFF[!redistribute]*X.next[!redistribute])
    
    rrr=matrix(NA,nrow=window,ncol=1)
    rrr= X_in[,!redistribute]%*%w_volFF[!redistribute]
    SR_outv_isFF[1]=sqrt(12)*apply(rrr, 2, mean)/apply(rrr, 2, sd)
    
    
    rrr_oos=matrix(NA,nrow=window,ncol=1)
    rrr_oos= X_oos[,!redistribute]%*%w_volFF[!redistribute]
    SR_outv_oosFF[1]=sqrt(12)*apply(rrr_oos, 2, mean)/apply(rrr_oos, 2, sd)
    
    
    
    p=0.05
    zp=qnorm(p)
    
    VaR_outv_isFF[j]=apply(rrr, 2, mean)+zp*apply(rrr, 2, sd)
    ES_outv_isFF[j]=apply(rrr, 2, mean)-(dnorm(zp)/p)*apply(rrr, 2, sd)
    
    VaR_outv_oosFF[j]=apply(rrr_oos, 2, mean)+zp*apply(rrr_oos, 2, sd)
    ES_outv_oosFF[j]=apply(rrr_oos, 2, mean)-(dnorm(zp)/p)*apply(rrr_oos, 2, sd)
    
    
    
    outv_rollFF[1] = sum(wFF[!redistribute]*X.next[!redistribute])
    
    
    
    gamFF= t(risk_premia.suppFF)%*%solve(omegaFF)%*%risk_premia.suppFF
    
    SR_outvFF2=gamFF
    VaR_outvFF2=gamFF*tar_vol  +zp*tar_vol
    ES_outvFF2=gamFF*tar_vol     -(dnorm(zp)/p)*tar_vol
    
    
    
    
    
    # 
    # if (l >= 2) 
    # { comandi }
    #i comandi nelle parentesi graffe saranno eseguiti solo se l >= 2.
    
    #  uncon
    nfacu = 5# ku[j]
    ## massimo
    # omegau = diag(1, nfacu) - gammak[1:nfacu] %*% t(gammak[1:nfacu])
    # sigma2u = windowu/(windowu - nfacu)*valuefu[nfacu]
    # 
    # Su = 1/sigma2u*diag(1, N)
    # w2u = solve(solve(omegau) + t(lambdau[-delete,1:nfacu]) %*% S %*% lambdau[-delete,1:nfacu])
    # tempu = lambdau[-delete,1:nfacu]
    # tempu = Su %*% tempu
    # w1u = Su - tempu %*% w2u %*% t(tempu)
    # 
    # # per il peso devo premoltiplicare per l'inverse della covariance matrix dei returns
    # # che e' uguale a (S^-1 + AOmegaA'). La inverto con sherman-morrison/woodbury
    # wu = w1u %*% lambdau[-delete,1:nfacu] %*% gammak[1:nfacu]
    # 
    # wu[redistribute] = 0
    # wu = wu/sum(wu)
    # 
    # outu[j] = sum(wu[!redistribute]*X.next[!redistribute])
    # # massimo
    
    omegauFF = cov(FF)# diag(1, nfacu) - gammak[1:nfacu] %*% t(gammak[1:nfacu])
    gammaFF=apply(FF,2,mean)
    #sigma2u = windowu/(windowu - nfacu)*valuefu[nfacu]
    #sigmalambdau=(t(lambdau[-delete,1:nfacu]) %*% lambdau[-delete,1:nfacu]) - (N/window)*sigma2u*diag(1,nfacu)
    sigmalambdauFF=(t(lambdauFF[-delete_oos,]) %*% lambdauFF[-delete_oos,])
    w2uFF = solve(sigma2FF*solve(omegauFF) + sigmalambdauFF)
    # aaa=t(gammak[1:nfacu])%*%solve(omegau)%*%w2u%*%sigmalambdau%*%w2u%*%solve(omegau)%*%gammak[1:nfacu]
    # 
    # if(aaa<=0)      { sigmalambdau=sigmalambdau1}
    # 
    # w2u=matrix(NA,nrow=nfacu, ncol=nfacu)
    #w2u = solve(sigma2u*solve(omegau) + sigmalambdau)
    wuFF =  lambdauFF[-delete_oos,] %*% w2uFF %*% solve(omegauFF)%*%gammaFF
    #wuFF[redistribute] = 0
    wu_tanFF = wuFF/sum(wuFF)
    outtuFF[1] = sum(wu_tanFF[!redistribute]*X.next[!redistribute])
    #outtuFF[j] = sum(wu_tanFF*X.next)
    
    
    rrr=matrix(NA,nrow=window,ncol=1)
    rrr= X_in[,!redistribute]%*%wu_tanFF[!redistribute]
    SR_outtu_isFF[1]=sqrt(12)*apply(rrr, 2, mean)/apply(rrr, 2, sd)
    p=0.05
    zp=qnorm(p)
    
    VaR_outtu_isFF[j]=apply(rrr, 2, mean)+zp*apply(rrr, 2, sd)
    ES_outtu_isFF[j]=apply(rrr, 2, mean)-(dnorm(zp)/p)*apply(rrr, 2, sd)
    
    
    
    #aaa=t(gammak[1:nfacu])%*%solve(omegau)%*%w2u%*%sigmalambdau%*%w2u%*%solve(omegau)%*%gammak[1:nfacu]
    volpuFF=sqrt(    t(gammaFF)%*%solve(omegauFF)%*%w2uFF%*%sigmalambdauFF%*%w2uFF%*%solve(omegauFF)%*%gammaFF)
    
    
    tar_vol=0.05/sqrt(12)
    wu_volFF =  (tar_vol)*wuFF%*%solve(volpuFF)
    outvuFF[1] = sum(wu_volFF[!redistribute]*X.next[!redistribute])
    
    outvu_rollFF[1] = sum(wuFF[!redistribute]*X.next[!redistribute])
    
    
    # end uncond
    
    
    
    
    wFF = 1/(N - sum(redistribute))
    outtFF[2] = wFF*sum(X.next[!redistribute])
    
    wFF = 1/(N - sum(redistribute))
    outvFF[2] = wFF*sum(X.next[!redistribute])
    
    wFF = 1/(N - sum(redistribute))
    outvuFF[2] = wFF*sum(X.next[!redistribute])
    
    wFF = 1/(N - sum(redistribute))
    outtuFF[2] = wFF*sum(X.next[!redistribute])
    
    
    
    rrr=matrix(NA,nrow=window,ncol=1)
    
    #rrr= X_in[,!redistribute]%*%w[!redistribute]
    Nnew=ncol(X_in[,!redistribute])
    rrr=  X_in[,!redistribute]%*%matrix(1,nrow=Nnew,ncol=1)/Nnew
    
    #SR_outt_isFF[1]=sqrt(12)*apply(rrr, 2, mean)/apply(rrr, 2, sd)
    #SR_outtu_isFF[1]=sqrt(12)*apply(rrr, 2, mean)/apply(rrr, 2, sd)
    
    
    #stop FF
    
    # list(outt,outv,outtu,outvu,outv_roll,outvu_roll,SR_outt_is,SR_outv_is,SR_outtu_is,#9
    #      outtFF,outvFF,outtuFF,outvuFF,outv_rollFF,outvu_rollFF,SR_outt_isFF,SR_outv_isFF,SR_outtu_isFF,#18
    #      CI_SR_plus2,CI_SR_minus2,CI_VaR_plus2,CI_VaR_minus2,CI_ES_plus2,CI_ES_minus2,#24
    #               VaR_outt_is,ES_outt_is,   
    #      VaR_outv_is2,ES_outv_is2,   
    #      VaR_outtu_is,ES_outtu_is,#30
    #      VaR_outt_isFF,ES_outt_isFF,   
    #      VaR_outv_isFF,ES_outv_isFF, #34  
    #               VaR_outtu_isFF,ES_outtu_isFF,SR_outv_is2,CI_SR_plus3,CI_SR_minus3,stat,statu,statew,statewu,pp,ppu,#43
    #      outvu_roll2,outvu2,ES_outtu_is2,VaR_outtu_is2,outtu2, outvu_roll3,outvu3,ES_outtu_is3,VaR_outtu_is3,outtu3,#53
    #      outvu_roll4,outvu4,ES_outtu_is4,VaR_outtu_is4,outtu4,#58
    #      SR_outvu_is2,VaR_outvu_is2,ES_outvu_is2,#63
    #      SR_outvFF2,VaR_outvFF2,ES_outvFF2,#64,65,66
    #      se_SR,se_VaR,se_ES,#69
    #      SR_outt_oos,
    #      VaR_outt_oos,
    #      ES_outt_oos,
    #      SR_outv_oos,
    #      SR_outtu_oos,
    #      VaR_outtu_oos,
    #      ES_outtu_oos,
    #      SR_outt_oosFF,
    #      VaR_outt_oosFF,
    #      ES_outt_oosFF,
    #      SR_outv_oosFF,
    #      VaR_outv_oosFF,
    #      ES_outv_oosFF,
    #      SR_outtu_oos2,
    #      VaR_outtu_oos2,
    #      ES_outtu_oos2)
    
    list(outt,#1
         SR_outt_is,
         SR_outt_oos,
         VaR_outt_is,
         ES_outt_is,
         VaR_outt_oos,
         ES_outt_oos,
         outv,
         SR_outv_is,
         SR_outv_oos,#10
         outv_roll,
         SR_outvu_is,
         VaR_outvu_is,
         ES_outvu_is,
         outtu,
         SR_outtu_is,
         SR_outtu_oos,
         VaR_outtu_is,
         ES_outtu_is,
         VaR_outtu_oos,#20
         ES_outtu_oos,
         outvu,
         outvu_roll,
         SR_outvu_is2,
         VaR_outvu_is2,
         ES_outvu_is2,
         outtu2,
         SR_outtu_is2,
         SR_outtu_oos2,
         VaR_outtu_is2,#30
         ES_outtu_is2,
         VaR_outtu_oos2,
         ES_outtu_oos2,
         outvu2,
         outvu_roll2,
         outtFF,
         SR_outt_isFF,
         SR_outt_oosFF,
         VaR_outt_isFF,
         ES_outt_isFF,#40
         VaR_outt_oosFF,
         ES_outt_oosFF,
         outvFF,
         SR_outv_isFF,
         VaR_outv_isFF,
         ES_outv_isFF,
         outv_rollFF,
         SR_outv_oosFF,
         VaR_outv_oosFF,
         ES_outv_oosFF,
         VaR_outv_is,
         ES_outv_is,
         VaR_outv_oos,
         ES_outv_oos)#54
    
    
  }
  # QQ
  # SR_outv_is2[j]=gam
  # VaR_outv_is2[j]=gam*tar_vol  +zp*tar_vol
  # ES_outv_is2[j]=gam*tar_vol     -(dnorm(zp)/p)*tar_vol
  # 
  # CI_SR_plus2[j]=gam+1.96*sqrt(se_SR2/N)#SR_outv_is[j]+1.96*sqrt(se_SR2/N)
  # CI_SR_minus2[j]=gam-1.96*sqrt(se_SR2/N)#SR_outv_is[j]-1.96*sqrt(se_SR2/N)
  # 
  # CI_SR_plus3[j]=SR_outv_is[j]+1.96*sqrt(12)*sqrt(se_SR2/N)
  # CI_SR_minus3[j]=SR_outv_is[j]-1.96*sqrt(12)*sqrt(se_SR2/N)
  # 
  # 
  # 
  # CI_VaR_plus2[j]= VaR_outv_is2[j]+1.96*sqrt(se_VaR2/N)
  # CI_VaR_minus2[j]= VaR_outv_is2[j]-1.96*sqrt(se_VaR2/N)
  # 
  # CI_ES_plus2[j]=ES_outv_is2[j]+1.96*sqrt(se_ES2/N)
  # CI_ES_minus2[j]=ES_outv_is2[j]-1.96*sqrt(se_ES2/N)
  # 
  # QQ
  
  oos_parallel_new = function(i, balancedN , data, rf, window, windowu, ts.eigenv , ts.deletestocks, ts.factors, ts.valuef, ts.nfactorhat, nfactorhatu, k0 , gammak , omegak , lambdau , valuefu, FF ,lambdau.suppFF  , ts.sigma4 , Fbal2v ,fhalfcon2v, Fabal2unbalv, fhalfcon2unbalv)
  {
    
    # QQQ
    #   #FF
    #   for (i in 1:(nw - 1)) ret.porttFF[, i + 1] = supp[[i]][[9+1]]
    #   for (i in 1:(nw - 1)) ret.portvFF[, i + 1] = supp[[i]][[9+2]]
    #   for (i in 1:(nw - 1)) ret.porttuFF[, i + 1] = supp[[i]][[9+3]]
    #   for (i in 1:(nw - 1)) ret.portvuFF[, i + 1] = supp[[i]][[9+4]]
    #   
    #   for (i in 1:(nw - 1))
    #   {ret.portv_rollFF[, i + 1] = supp[[i]][[9+5]]
    #   }
    #   
    #   for (i in 1:(nw - 1))
    #   {ret.portvu_rollFF[, i + 1] = supp[[i]][[9+6]]
    #   }
    #   for (i in 1:(nw-1)) SR.porttFF[i+1,]  =  supp[[i]][[9+7]]
    #   for (i in 1:(nw - 1)) SR.portvFF[i + 1,]  =  supp[[i]][[9+8]]
    #   for (i in 1:(nw - 1)) SR.porttuFF[i + 1,]  =  supp[[i]][[9+9]]
    #   SR.porttFF[1,]=matrix(0,nrow=1,ncol=7)
    #   SR.porttuFF[1,]=matrix(0,nrow=1,ncol=7)   
    #   SR.portvFF[1,]=matrix(0,nrow=1,ncol=7)  
    
  #   lambdauFF=lambdau.suppFF
  #   
    index = i:(i + window-1)
  #  # index_oos = (i+window):(i + 2*window-1)
  #   
     rf.supp = rf[index] - 1
  
    X_in2=data[index,]-rf.supp%*%t(rep(1,ncol(data))) # i : i+w-1
    X_next2=data[i+window,]-(rf[i+window]-1)%*%t(rep(1,ncol(data)))
    X_nownext=rbind(X_in2,X_next2)
    delete_nownext = apply(X_nownext, 2, function(x){sum(is.na(x))})
    delete_nownext = which(delete_nownext > 0)
    
    
    N_nownext=ncol(X_nownext[,-delete_nownext])
    # this is from i to i+windows so size window+1
    X_nownext = X_nownext[,-delete_nownext]
    #X_nownext = X_nownext -(rf[i:(i+window)]-1)%*%t(rep(1,N_nownext))
    X_next=X_nownext[window+1,]
    
    
    # bal data
    
    delete_bal = apply(data, 2, function(x){sum(is.na(x))})
    delete_bal = which(delete_bal > 0)
    N_bal=ncol(data[,-delete_bal])
    
    X_nownext1=data[index,-delete_bal]-rf.supp%*%t(rep(1,N_bal)) # i : i+w-1
    X_nownext2=data[i+window,-delete_bal]-(rf[i+window]-1)%*%t(rep(1,N_bal))
    X_nownext_bal=rbind(X_nownext1,X_nownext2)
    X_next_bal=X_nownext2
    
    
      
    
    
    #X_balanced_local = data[index,-delete_bal]
    #N_bal = ncol(X_balanced_local)
    #X_balanced_local = as.matrix(X_balanced_local) - rf.supp %*% t(rep(1, N_bal))
    
    
    
    # lead=window+1
    # X.next=matrix(NA,nrow=1,ncol=N)
    # X.next = data[i+window,-delete_oos]-(rf[i+window]-1)%*%t(rep(1,N))
    # X_in=X
    
    
    # factor.supp = ts.factors[,,i]
    # lambda.supp = t(1/window *(t(factor.supp) %*% X))
    # 
    # risk_premia.supp = apply(factor.supp, 2, mean)
    # valuef.supp = ts.valuef[, i]
    # 
    # k = c(ts.nfactorhat[i], k0)
    # ku = c(nfactorhatu, k0)
    #X.next = data[window + i,]
    
    # if (!is.na(ts.deletestocks[[1]][1]))
    #X.next =matrix(X.next[-delete],nrow=1,ncol=N)
    #X.next =X.next[-delete]
    #redistribute = is.na(X.next)
    
    
    
    # if (!is.na(ts.deletestocks[[1]][1]))    
    #X_in=X_in[,-delete]
    # #redistribute_is = is.na(X_in)
    # N_in=ncol(X_in)
    # N_next=ncol(X.next)
    # 
    
    
    

    
    
    
  #  j=1  
  #  for (j in 1:length(k))
      # j=1 
  #  {
      
      
   # nfac = k[j]
   nfac= ts.nfactorhat[i]
    
   
      
      # cond bal
      
      totfac2=rbind(as.matrix(Fbal2v[2:window,,(i+1)],window-1,10),t(fhalfcon2v[,(i+1)]))
      totfac2=matrix(totfac2,window,nfac)
      lam.supp=t(X_nownext_bal[1:window,])%*%totfac2%*%solve(t(totfac2)%*%totfac2) 
      #Z=lam.supp%*%apply(totfac2,2,mean)
      
      #X_nownext = X_nownext -(rf[i:(i+window)]-1)%*%t(rep(1,N_nownext))
      #X_next=X_nownext[window+1,]
      omega=var(totfac2)
      risk.supp=apply(totfac2,2,mean)
      
      
      
      
      val=0
      val = 1/(N_bal*window) * sum((X_nownext_bal[1:window,] -   totfac2 %*% t(lam.supp))^2)
      sigma2=0
      sigma2=(window/(window - nfac))*val
      sigmalambda=matrix(NA,nfac,nfac)
      sigmalambda1=matrix(NA,nfac,nfac)
      sigmalambda=(t(lam.supp) %*% lam.supp) - (N_bal/window)*sigma2*diag(1,nfac)
      sigmalambda1=(t(lam.supp) %*% lam.supp)
      w2 = solve(sigma2*solve(omega) + sigmalambda)
      
      aaa=( t(risk.supp)%*%solve(omega)%*%w2%*%sigmalambda%*%w2%*%solve(omega)%*%risk.supp)
      
      
      if(aaa<=0)     { sigmalambda=sigmalambda1}      
      
      w2=matrix(NA,nrow=nfac, ncol=nfac)
      
      w2 = solve(sigma2*solve(omega) + sigmalambda)
      w22 =  lam.supp %*% w2 %*% solve(omega)%*%risk.supp
      #w22[redistribute] = 0
      w_tan = w22/sum(w22)
      #outt_unc_oos[j] = sum(w_tan[!redistribute]*Xhalf[!redistribute])
      
      gL= (w2%*%solve(omega)%*%risk.supp)/as.numeric(sum(w22)) 
      
      rrr_con_oos= X_nownext_bal[(window+1),]%*%w_tan - (N_bal/window)*sigma2*totfac2[window,]%*%gL
      
      rrr_con_oos2=X_nownext_bal[(window+1),]%*%w_tan
      
      # target vol
      if (nfac==1)
      { 
        volp=sqrt( t(risk.supp)%*%solve(omega[1,1])%*%w2%*%t(lam.supp)%*%
                     (omega[1,1]*lam.supp%*%t(lam.supp) +sigma2*diag(N_bal))%*%lam.supp%*%
                     w2%*%solve(omega[1,1])%*%risk.supp)
      } else { volp=sqrt( t(risk.supp)%*%solve(omega)%*%w2%*%t(lam.supp)%*%
                            (lam.supp%*%omega%*%t(lam.supp) +sigma2*diag(N_bal))%*%lam.supp%*%
                            w2%*%solve(omega)%*%risk.supp)}
      tar_vol=0.05/sqrt(12)
      w_vol = (tar_vol)*w22%*%solve(volp)
      #outv[,j] = sum(w_vol[!redistribute]*X.next[!redistribute])
      
      gLv= tar_vol*(w2%*%solve(omega)%*%risk.supp)/as.numeric(volp)
      #      as.numeric((sqrt(t(risk_premia.supp[1:nfac])%*%solve(omega)%*%risk_premia.supp[1:nfac]))) 
      
      rrr_conv_oos=   X_nownext_bal[(window+1),]%*%w_vol - (N_bal/window)*sigma2*totfac2[window,]%*%gLv

      rrr_conv_oos2=  X_nownext_bal[(window+1),]%*%w_vol 
      
  #  }
      
      # con unbal e unbal2
      
      
    totfac22=rbind(as.matrix(Fabal2unbalv[2:window,,(i+1)],window-1,10),t(fhalfcon2unbalv[,(i+1)]))
    totfac22=matrix(totfac22,window,nfac)
    lam.supp22=t(X_nownext[1:window,])%*%totfac22%*%solve(t(totfac22)%*%totfac22)
    lam.supp2=t(X_nownext[1:window,])%*%ts.factors[,1:nfac,i]%*%solve(t(ts.factors[,1:nfac,i])%*%ts.factors[,1:nfac,i]) 
    
    
    
    # unbal
    
    omega=var(totfac22)
    risk.supp=apply(totfac22,2,mean)
    
    
    
    
    val=0
    val = 1/(N_nownext*window) * sum((X_nownext[1:window,] -   totfac22 %*% t(lam.supp22))^2)
    sigma2=0
    sigma2=(window/(window - nfac))*val
    sigmalambda=matrix(NA,nfac,nfac)
    sigmalambda1=matrix(NA,nfac,nfac)
    sigmalambda=(t(lam.supp22) %*% lam.supp22) - (N_nownext/window)*sigma2*diag(1,nfac)
    sigmalambda1=(t(lam.supp22) %*% lam.supp22)
    w2 = solve(sigma2*solve(omega) + sigmalambda)
    
    aaa=( t(risk.supp)%*%solve(omega)%*%w2%*%sigmalambda%*%w2%*%solve(omega)%*%risk.supp)
    
    
    if(aaa<=0)     { sigmalambda=sigmalambda1}      
    
    w2=matrix(NA,nrow=nfac, ncol=nfac)
    
    w2 = solve(sigma2*solve(omega) + sigmalambda)
    w22 =  lam.supp22 %*% w2 %*% solve(omega)%*%risk.supp
    #w22[redistribute] = 0
    w_tan = w22/sum(w22)
    #outt_unc_oos[j] = sum(w_tan[!redistribute]*Xhalf[!redistribute])
    
    gL= (w2%*%solve(omega)%*%risk.supp)/as.numeric(sum(w22)) 
    
    rrr_con_unbal_oos= X_nownext[(window+1),]%*%w_tan - (N_nownext/window)*sigma2*totfac22[window,]%*%gL
    
    rrr_con_unbal_oos2=X_nownext[(window+1),]%*%w_tan
    
    # target vol
    if (nfac==1)
    { 
      volp=sqrt( t(risk.supp)%*%solve(omega[1,1])%*%w2%*%t(lam.supp22)%*%
                   (omega[1,1]*lam.supp22%*%t(lam.supp22) +sigma2*diag(N_nownext))%*%lam.supp22%*%
                   w2%*%solve(omega[1,1])%*%risk.supp)
    } else { volp=sqrt( t(risk.supp)%*%solve(omega)%*%w2%*%t(lam.supp22)%*%
                          (lam.supp22%*%omega%*%t(lam.supp22) +sigma2*diag(N_nownext))%*%lam.supp22%*%
                          w2%*%solve(omega)%*%risk.supp)}
    tar_vol=0.05/sqrt(12)
    w_vol = (tar_vol)*w22%*%solve(volp)
    #outv[,j] = sum(w_vol[!redistribute]*X.next[!redistribute])
    
    gLv= tar_vol*(w2%*%solve(omega)%*%risk.supp)/as.numeric(volp)
    #      as.numeric((sqrt(t(risk_premia.supp[1:nfac])%*%solve(omega)%*%risk_premia.supp[1:nfac]))) 
    
    rrr_conv_unbal_oos=   X_nownext[(window+1),]%*%w_vol - (N_nownext/window)*sigma2*totfac22[window,]%*%gLv
    
    rrr_conv_unbal_oos2=  X_nownext[(window+1),]%*%w_vol 
    
    
    # unbal2

     
   
    omega=var(ts.factors[,1:nfac,i])
   if (nfac==1)
{   risk.supp= mean(ts.factors[,1:nfac,i]) } else
{      risk.supp=apply(ts.factors[,1:nfac,i],2,mean) }
    
    
    #lam.supp2=t(X_nownext[1:window,])%*%ts.factors[,1:nfac,i]/N_nownext 
    
    
    val=0
    val = 1/(N_nownext*window) * sum((X_nownext[1:window,] -   ts.factors[,1:nfac,i]%*% t(lam.supp2))^2)
    sigma2=0
    sigma2=window/(window - nfac)*val
    sigmalambda=matrix(NA,nfac,nfac)
    sigmalambda1=matrix(NA,nfac,nfac)
    sigmalambda=(t(lam.supp2) %*% lam.supp2) - (N_nownext/window)*sigma2*diag(1,nfac)
    sigmalambda1=(t(lam.supp2) %*% lam.supp2)
    w2 = solve(sigma2*solve(omega) + sigmalambda)
    
    aaa=( t(risk.supp)%*%solve(omega)%*%w2%*%sigmalambda%*%w2%*%solve(omega)%*%risk.supp)
    
    
    if(aaa<=0)     { sigmalambda=sigmalambda1}      
    
    w2=matrix(NA,nrow=nfac, ncol=nfac)
    
    w2 = solve(sigma2*solve(omega) + sigmalambda)
    w22 =  lam.supp2 %*% w2 %*% solve(omega)%*%risk.supp
    #w22[redistribute] = 0
    w_tan = w22/sum(w22)
    #outt_unc_oos[j] = sum(w_tan[!redistribute]*Xhalf[!redistribute])
    
    gL= (w2%*%solve(omega)%*%risk.supp)/as.numeric(sum(w22)) 
    
    rrr_con_unbal2_oos= X_nownext[(window+1),]%*%w_tan - (N_nownext/window)*sigma2*ts.factors[window,1:nfac,i]%*%gL
    
    rrr_con_unbal2_oos2=X_nownext[(window+1),]%*%w_tan
    
    # target vol
    if (nfac==1)
    { 
      volp=sqrt( t(risk.supp)%*%solve(omega)%*%w2%*%t(lam.supp2)%*%
                   (omega*lam.supp2%*%t(lam.supp2) +sigma2*diag(N_nownext))%*%lam.supp2%*%
                   w2%*%solve(omega)%*%risk.supp)
    } else { volp=sqrt( t(risk.supp)%*%solve(omega)%*%w2%*%t(lam.supp2)%*%
                          (lam.supp2%*%omega%*%t(lam.supp2) +sigma2*diag(N_nownext))%*%lam.supp2%*%
                          w2%*%solve(omega)%*%risk.supp)}
    tar_vol=0.05/sqrt(12)
    w_vol = (tar_vol)*w22%*%solve(volp)
    #outv[,j] = sum(w_vol[!redistribute]*X.next[!redistribute])
    
    gLv= tar_vol*(w2%*%solve(omega)%*%risk.supp)/as.numeric(volp)
    #      as.numeric((sqrt(t(risk_premia.supp[1:nfac])%*%solve(omega)%*%risk_premia.supp[1:nfac]))) 
    
    rrr_conv_unbal2_oos=   X_nownext[(window+1),]%*%w_vol - (N_nownext/window)*sigma2*ts.factors[window,1:nfac,i]%*%gLv
    
    rrr_conv_unbal2_oos2=  X_nownext[(window+1),]%*%w_vol 
    
      
  
      
    
    
    list(rrr_con_oos,rrr_con_oos2,rrr_conv_oos,rrr_conv_oos2,rrr_con_unbal_oos,rrr_con_unbal_oos2,rrr_conv_unbal_oos,rrr_conv_unbal_oos2,
    rrr_con_unbal2_oos,rrr_con_unbal2_oos2,rrr_conv_unbal2_oos,rrr_conv_unbal2_oos2)
    
  }
  
  
  oos_parallel_new2 = function(i, balancedN , data, rf, window, windowu, ts.eigenv , ts.deletestocks, ts.factors, ts.valuef, ts.nfactorhat, nfactorhatu, k0 , gammak , omegak , lambdau , valuefu, FF ,lambdau.suppFF  , ts.sigma4 , Fbal2v ,fhalfcon2v, Fabal2unbalv, fhalfcon2unbalv)
  {
    
    # QQQ
    #   #FF
    #   for (i in 1:(nw - 1)) ret.porttFF[, i + 1] = supp[[i]][[9+1]]
    #   for (i in 1:(nw - 1)) ret.portvFF[, i + 1] = supp[[i]][[9+2]]
    #   for (i in 1:(nw - 1)) ret.porttuFF[, i + 1] = supp[[i]][[9+3]]
    #   for (i in 1:(nw - 1)) ret.portvuFF[, i + 1] = supp[[i]][[9+4]]
    #   
    #   for (i in 1:(nw - 1))
    #   {ret.portv_rollFF[, i + 1] = supp[[i]][[9+5]]
    #   }
    #   
    #   for (i in 1:(nw - 1))
    #   {ret.portvu_rollFF[, i + 1] = supp[[i]][[9+6]]
    #   }
    #   for (i in 1:(nw-1)) SR.porttFF[i+1,]  =  supp[[i]][[9+7]]
    #   for (i in 1:(nw - 1)) SR.portvFF[i + 1,]  =  supp[[i]][[9+8]]
    #   for (i in 1:(nw - 1)) SR.porttuFF[i + 1,]  =  supp[[i]][[9+9]]
    #   SR.porttFF[1,]=matrix(0,nrow=1,ncol=7)
    #   SR.porttuFF[1,]=matrix(0,nrow=1,ncol=7)   
    #   SR.portvFF[1,]=matrix(0,nrow=1,ncol=7)  
    
    #   lambdauFF=lambdau.suppFF
    #   
    index = i:(i + window-1)
    #  # index_oos = (i+window):(i + 2*window-1)
    #   
    rf.supp = rf[index] - 1
    # #  rf.oos = rf[index_oos] - 1
    #   
    #   X = data[index,]
    #  X_oos=data[index_oos,]
    
    
    
    # X_temp = data[c(index, index_oos),]
    #  delete_oos = apply(X_temp, 2, function(x){sum(is.na(x))})
    #  delete_oos = which(delete_oos > 0)
    
    #    delete_in = apply(X, 2, function(x){sum(is.na(x))})
    #    delete_in = which(delete > 0)
    #  
    #  
    # X = X[,-delete_in]
    #  
    #  N = ncol(X)
    #  X = as.matrix(X) - rf.supp %*% t(rep(1, N))
    
    
    # X_oos = X_oos[,-delete_oos]
    
    # N_oos = ncol(X_oos)
    #X_oos = as.matrix(X_oos) - rf.oos %*% t(rep(1, N_oos))
    
    
    
    #N_oos = ncol(X_oos)
    #X_oos = as.matrix(X_oos) - rf.oos %*% t(rep(1, N_oos))
    
    
    
    
    X_in2=data[index,]-rf.supp%*%t(rep(1,ncol(data))) # i : i+w-1
    X_next2=data[i+window,]-(rf[i+window]-1)%*%t(rep(1,ncol(data)))
    X_nownext=rbind(X_in2,X_next2)
    delete_nownext = apply(X_nownext, 2, function(x){sum(is.na(x))})
    delete_nownext = which(delete_nownext > 0)
    
    
    N_nownext=ncol(X_nownext[,-delete_nownext])
    # this is from i to i+windows so size window+1
    X_nownext = X_nownext[,-delete_nownext]
    #X_nownext = X_nownext -(rf[i:(i+window)]-1)%*%t(rep(1,N_nownext))
    X_next=X_nownext[window+1,]
    
    
    # bal data
    
    delete_bal = apply(data, 2, function(x){sum(is.na(x))})
    delete_bal = which(delete_bal > 0)
    N_bal=ncol(data[,-delete_bal])
    X_balanced1=data[,-delete_bal]-(rf-1)%*%t(rep(1,N_bal))
    
    X_nownext1=data[index,-delete_bal]-rf.supp%*%t(rep(1,N_bal)) # i : i+w-1
    X_nownext2=data[i+window,-delete_bal]-(rf[i+window]-1)%*%t(rep(1,N_bal))
    X_nownext_bal=rbind(X_nownext1,X_nownext2)
    X_next_bal=X_nownext2
    
    
    # equally weighted bal
    
    rrr_bal_1N=sum(X_balanced1[i+window,])/N_bal
    
    # equally weighted nonbal
   
    rrr_unb_1N=sum(X_next)/N_nownext
    
    
    
    
    #X_balanced_local = data[index,-delete_bal]
    #N_bal = ncol(X_balanced_local)
    #X_balanced_local = as.matrix(X_balanced_local) - rf.supp %*% t(rep(1, N_bal))
    
    
    
    # lead=window+1
    # X.next=matrix(NA,nrow=1,ncol=N)
    # X.next = data[i+window,-delete_oos]-(rf[i+window]-1)%*%t(rep(1,N))
    # X_in=X
    
    
    # factor.supp = ts.factors[,,i]
    # lambda.supp = t(1/window *(t(factor.supp) %*% X))
    # 
    # risk_premia.supp = apply(factor.supp, 2, mean)
    # valuef.supp = ts.valuef[, i]
    # 
    # k = c(ts.nfactorhat[i], k0)
    # ku = c(nfactorhatu, k0)
    #X.next = data[window + i,]
    
    # if (!is.na(ts.deletestocks[[1]][1]))
    #X.next =matrix(X.next[-delete],nrow=1,ncol=N)
    #X.next =X.next[-delete]
    #redistribute = is.na(X.next)
    
    
    
    # if (!is.na(ts.deletestocks[[1]][1]))    
    #X_in=X_in[,-delete]
    # #redistribute_is = is.na(X_in)
    # N_in=ncol(X_in)
    # N_next=ncol(X.next)
    # 
    
    
    
    
    
    
    
    #  j=1  
    #  for (j in 1:length(k))
    # j=1 
    #  {
    
    
    # nfac = k[j]
  
    
    # uncond bal forecast for i+w from i:i+w-1
    
    #factoru_halfT = sqrt(Thalf) * as.matrix(eigen(X_balanced[1:Thalf,] %*% t(X_balanced[1:Thalf,]))$vectors[,1:10])
    #riskpremiau_halfT=matrix(apply( factoru_halfT,2,mean),10,1)
    Thalf=nrow(data)/2
    lambdau_halfT=sqrt(N_bal) * as.matrix(eigen(t(X_balanced1[1:Thalf,]) %*% (X_balanced1[1:Thalf,]))$vectors[,1:10])
    factoru_halfT = (X_balanced1[1:Thalf,] %*%  lambdau_halfT)/N_bal
    riskpremiau_halfT=matrix(apply( factoru_halfT,2,mean),10,1)
    omega_halfT=var(factoru_halfT)
    
   
     factor_uncond_oos =( X_nownext2 %*%  lambdau_halfT)/N_bal # i+w
     rrr_unc_oos=matrix(0,10,1)
     rrr_unc_oos2=matrix(0,10,1)
     rrr_unc_oos3=matrix(0,10,1)
     rrr_unc_oos4=matrix(0,10,1)
     rrr_unc_oos5=matrix(0,10,1)
     
     
     j=1
     for (j in 1:10)
     {  
       oo=solve(omega_halfT[1:j,1:j])
         val=0
         val=  (1/(N_bal*Thalf) ) * sum((X_balanced1[1:Thalf,] -  factoru_halfT[,1:j] %*% t(lambdau_halfT[,1:j]))^2)
         sigma2=0
         sigma2=(Thalf/(Thalf - j))*val
         
         sigmalambda=matrix(NA,j,j)
         sigmalambda1=matrix(NA,j,j)
         sigmalambda=(t(lambdau_halfT[,1:j]) %*% lambdau_halfT[,1:j]) - (N_bal/Thalf)*sigma2*diag(1,j)
         sigmalambda1=(t(lambdau_halfT[,1:j]) %*% lambdau_halfT[,1:j])
         w2 = solve(sigma2*oo + sigmalambda)
         
         aaa=( t(riskpremiau_halfT[1:j])%*%oo%*%w2%*%sigmalambda%*%w2%*%oo%*%riskpremiau_halfT[1:j])
         
         
         if(aaa<=0)     { sigmalambda=sigmalambda1}      
         
         w2=matrix(NA,nrow=j, ncol=j)
         
         w2 = solve(sigma2*oo + sigmalambda)
         w22 = lambdau_halfT[,1:j]  %*% w2 %*% oo %*%riskpremiau_halfT[1:j]
         #w22[redistribute] = 0
         w_tan = w22/sum(w22)
         #outt_unc_oos[j] = sum(w_tan[!redistribute]*Xhalf[!redistribute])
         
         gL= (w2%*%oo%*%riskpremiau_halfT[1:j])/as.numeric(sum(w22)) 
         
         rrr_unc_oos[j]= X_nownext2%*%w_tan # - (N_bal/Thalf)*sigma2*totfac2[window,]%*%gL
         
         w_mv = w22/as.numeric( matrix(riskpremiau_halfT[1:j],1,j)%*%t( lambdau_halfT[,1:j])%*% lambdau_halfT[,1:j]  %*% w2 %*% oo %*%riskpremiau_halfT[1:j] )
         rrr_unc_oos2[j]= X_nownext2%*%w_mv # - (N_bal/Thalf)*sigma2*totfac2[window,]%*%gL
         
          
         
         rrr_unc_oos3[j]= factor_uncond_oos[1:j]%*%oo%*% riskpremiau_halfT[1:j]/
           (sum(oo%*% riskpremiau_halfT[1:j]) )# - (N_bal/Thalf)*sigma2*totfac2[window,]%*%gL
         rrr_unc_oos4[j]= factor_uncond_oos[1:j]%*%oo%*% riskpremiau_halfT[1:j]/
           (matrix(riskpremiau_halfT[1:j],1,j)%*%oo%*% riskpremiau_halfT[1:j] )# - (N_bal/Thalf)*sigma2*totfac2[window,]%*%gL
         
           
         
         
       
         }
    
    
      
     rrr_unc_oos5= factor_uncond_oos  
    
    
     # uncond is
     
     # estimate full sample?
     Tfull=nrow(data)
     Thalf=nrow(data)/2
     lambdau_halfT2=sqrt(N_bal) * as.matrix(eigen(t(X_balanced1[(Thalf+1):Tfull,]) %*% (X_balanced1[(Thalf+1):Tfull,]))$vectors[,1:10])
     factoru_halfT2 = (X_balanced1[(Thalf+1):Tfull,] %*%  lambdau_halfT2)/N_bal
     riskpremiau_halfT2=matrix(apply( factoru_halfT2,2,mean),10,1)
     omega_halfT2=var(factoru_halfT2)
     
     
     factor_uncond_is =   ( X_balanced1[i+window-1,] %*%  lambdau_halfT2)/N_bal # i+w
     factor_uncond_is_vec =   ( X_balanced1[i:(i+window-1),] %*%  lambdau_halfT2)/N_bal # i+w
     
      rrr_unc_is1=matrix(0,10,1)
     rrr_unc_is2=matrix(0,10,1)
     rrr_unc_is3=matrix(0,10,1)
     rrr_unc_is4=matrix(0,10,1)
     rrr_unc_is5=matrix(0,10,1)
     
     rrr_unc_is1_vec=matrix(0,window,10)
     rrr_unc_is2_vec=matrix(0,window,10)
     rrr_unc_is3_vec=matrix(0,window,10)
     rrr_unc_is4_vec=matrix(0,window,10)
     rrr_unc_is5_vec=matrix(0,window,10)
     
     
     
     j=1
     for (j in 1:10)
     {  
       oo=solve(omega_halfT2[1:j,1:j])
       val=0
       val=  (1/(N_bal*Thalf) ) * sum((X_balanced1[(Thalf+1):Tfull,] -  factoru_halfT2[,1:j] %*% t(lambdau_halfT2[,1:j]))^2)
       sigma2=0
       sigma2=(Thalf/(Thalf - j))*val
       
       sigmalambda=matrix(NA,j,j)
       sigmalambda1=matrix(NA,j,j)
       sigmalambda=(t(lambdau_halfT2[,1:j]) %*% lambdau_halfT2[,1:j]) - (N_bal/Thalf)*sigma2*diag(1,j)
       sigmalambda1=(t(lambdau_halfT2) %*% lambdau_halfT2[,1:j])
       w2 = solve(sigma2*oo + sigmalambda)
       
       aaa=( t(riskpremiau_halfT2[1:j])%*%oo%*%w2%*%sigmalambda%*%w2%*%oo%*%riskpremiau_halfT2[1:j])
       
       
       if(aaa<=0)     { sigmalambda=sigmalambda1}      
       
       w2=matrix(NA,nrow=j, ncol=j)
       
       w2 = solve(sigma2*oo + sigmalambda)
       w22 = lambdau_halfT2[,1:j]  %*% w2 %*% oo %*%riskpremiau_halfT2[1:j]
       #w22[redistribute] = 0
       w_tan = w22/sum(w22)
       #outt_unc_oos[j] = sum(w_tan[!redistribute]*Xhalf[!redistribute])
       
       gL= (w2%*%oo%*%riskpremiau_halfT2[1:j])/as.numeric(sum(w22)) 
       
       rrr_unc_is1[j]= X_balanced1[i+window-1,]%*%w_tan # - (N_bal/Thalf)*sigma2*totfac2[window,]%*%gL
       rrr_unc_is1_vec[,j]= X_balanced1[index,]%*%w_tan
       
       
       w_mv = w22/as.numeric( matrix(riskpremiau_halfT2[1:j],1,j)%*%t( lambdau_halfT2[,1:j])%*% lambdau_halfT2[,1:j]  %*% w2 %*% oo %*%riskpremiau_halfT2[1:j] )
       rrr_unc_is2[j]= X_balanced1[i+window-1,]%*%w_mv # - (N_bal/Thalf)*sigma2*totfac2[window,]%*%gL
       rrr_unc_is2_vec[,j]= X_balanced1[index,]%*%w_mv
       
       
       
       rrr_unc_is3[j]= factor_uncond_is[1:j]%*%oo%*% riskpremiau_halfT2[1:j]/
         (sum(oo%*% riskpremiau_halfT2[1:j]) )# - (N_bal/Thalf)*sigma2*totfac2[window,]%*%gL
       rrr_unc_is3_vec[,j]= factor_uncond_is_vec[,1:j]%*%oo%*% riskpremiau_halfT2[1:j]/
         (sum(oo%*% riskpremiau_halfT2[1:j]) )
         
         
       
       rrr_unc_is4[j]= factor_uncond_is[1:j]%*%oo%*% riskpremiau_halfT2[1:j]/
         (matrix(riskpremiau_halfT2[1:j],1,j)%*%oo%*% riskpremiau_halfT2[1:j] )# - (N_bal/Thalf)*sigma2*totfac2[window,]%*%gL
       rrr_unc_is4_vec[,j]= 
         matrix(factor_uncond_is_vec[,1:j],window,j)%*%oo%*% riskpremiau_halfT2[1:j]/
           as.numeric(matrix(riskpremiau_halfT2[1:j],1,j)%*%oo%*% riskpremiau_halfT2[1:j])# - (N_bal/Thalf)*sigma2*totfac2[window,]%*%gL
       
       
       
       
     }
     
     
     
     rrr_unc_is5= factor_uncond_is  
     rrr_unc_is5_vec= factor_uncond_is_vec
     
     
     
    
    
    # cond bal
    
    #   oos+1 
     

     
     lambda.supp=sqrt(N_bal) * as.matrix(eigen(t(X_balanced1[index,]) %*% (X_balanced1[index,]))$vectors[,1:10])
     factor.supp = (X_balanced1[index,] %*%  lambda.supp)/N_bal
     riskpremia.supp=matrix(apply(factor.supp,2,mean),10,1)
     omega.supp=var(factor.supp)
     
     
     factor.supp_oos =( X_nownext2 %*%  lambda.supp)/N_bal # i+w
     rrr_con_oos1=matrix(0,10,1)
     rrr_con_oos2=matrix(0,10,1)
     rrr_con_oos3=matrix(0,10,1)
     rrr_con_oos4=matrix(0,10,1)
     rrr_con_oos5=matrix(0,10,1)
     
     
     j=1
     for (j in 1:10)
     {  
       oo=solve(omega.supp[1:j,1:j])
       val=0
       val=  (1/(N_bal*window) ) * sum((X_balanced1[index,] -  factor.supp[,1:j] %*% t(lambda.supp[,1:j]))^2)
       sigma2=0
       sigma2=(window/(window - j))*val
       
       sigmalambda=matrix(NA,j,j)
       sigmalambda1=matrix(NA,j,j)
       sigmalambda=(t(lambda.supp[,1:j]) %*% lambda.supp[,1:j]) - (N_bal/window)*sigma2*diag(1,j)
       sigmalambda1=(t(lambda.supp[,1:j]) %*% lambda.supp[,1:j])
       w2 = solve(sigma2*oo + sigmalambda)
       
       aaa=( t(riskpremia.supp[1:j])%*%oo%*%w2%*%sigmalambda%*%w2%*%oo%*%riskpremia.supp[1:j])
       
       
       if(aaa<=0)     { sigmalambda=sigmalambda1}      
       
       w2=matrix(NA,nrow=j, ncol=j)
       
       w2 = solve(sigma2*oo + sigmalambda)
       w22 = lambda.supp[,1:j]  %*% w2 %*% oo %*%riskpremia.supp[1:j]
       #w22[redistribute] = 0
       w_tan = w22/sum(w22)
       #outt_unc_oos[j] = sum(w_tan[!redistribute]*Xhalf[!redistribute])
       
       gL= (w2%*%oo%*%riskpremia.supp[1:j])/as.numeric(sum(w22)) 
       
       rrr_con_oos1[j]= X_nownext2%*%w_tan # - (N_bal/Thalf)*sigma2*totfac2[window,]%*%gL
       
       w_mv = w22/as.numeric( matrix(riskpremia.supp[1:j],1,j)%*%t( lambda.supp[,1:j])%*% lambda.supp[,1:j]  %*% w2 %*% oo %*%riskpremia.supp[1:j] )
       rrr_con_oos2[j]= X_nownext2%*%w_mv # - (N_bal/Thalf)*sigma2*totfac2[window,]%*%gL
       
       
       
       rrr_con_oos3[j]= factor.supp_oos[1:j]%*%oo%*% riskpremia.supp[1:j]/
         (sum(oo%*% riskpremia.supp[1:j]) )# - (N_bal/Thalf)*sigma2*totfac2[window,]%*%gL
      
       
       
       
        rrr_con_oos4[j]= factor.supp_oos[1:j]%*%oo%*% riskpremia.supp[1:j]/
         (matrix(riskpremia.supp[1:j],1,j)%*%oo%*% riskpremia.supp[1:j] )# - (N_bal/Thalf)*sigma2*totfac2[window,]%*%gL
       
       
       
       
       
     }
     nfac= ts.nfactorhat[i]
     ooo=solve(var(factor.supp[,1:nfac]))
     rrr_con_oos3due= factor.supp_oos[1:nfac]%*%ooo%*% riskpremia.supp[1:nfac]/
       (sum(ooo%*% riskpremia.supp[1:nfac]) )# - (N_bal/Thalf)*sigma2*totfac2[window,]%*%gL
     
     
     rrr_con_oos5= factor.supp_oos  
    
     
     
   # cond is bal
     
     
     # lambda.supp=sqrt(N_bal) * as.matrix(eigen(t(X_balanced1[index,]) %*% (X_balanced1[index,]))$vectors[,1:10])
     # factor.supp = (X_balanced1[index,] %*%  lambda.supp)/N_bal
     # riskpremia.supp=matrix(apply(factor.supp,2,mean),10,1)
     # omega.supp=var(factor.supp)
     # factor.supp_oos =( X_nownext2 %*%  lambda.supp)/N_bal # i+w
     # 
     
     
     lambda.supp=sqrt(N_bal) * as.matrix(eigen(t(X_balanced1[index,]) %*% (X_balanced1[index,]))$vectors[,1:10])
     factor.supp = (X_balanced1[index,] %*%  lambda.supp)/N_bal
     riskpremia.supp=matrix(apply(factor.supp,2,mean),10,1)
     omega.supp=var(factor.supp)
     
     
   #  factor.supp_oos =( X_nownext2 %*%  lambda.supp)/N_bal # i+w
     rrr_con_is1=matrix(0,10,1)
     rrr_con_is2=matrix(0,10,1)
     rrr_con_is3=matrix(0,10,1)
     rrr_con_is4=matrix(0,10,1)
     rrr_con_is5=matrix(0,10,1)
     
     rrr_con_is1_vec=matrix(0,window,10)
     rrr_con_is2_vec=matrix(0,window,10)
     rrr_con_is3_vec=matrix(0,window,10)
     rrr_con_is4_vec=matrix(0,window,10)
     rrr_con_is5_vec=matrix(0,window,10)
     
     
          
     j=1
     for (j in 1:10)
     {  
       oo=solve(omega.supp[1:j,1:j])
       val=0
       val=  (1/(N_bal*window) ) * sum((X_balanced1[index,] -  factor.supp[,1:j] %*% t(lambda.supp[,1:j]))^2)
       sigma2=0
       sigma2=(window/(window - j))*val
       
       sigmalambda=matrix(NA,j,j)
       sigmalambda1=matrix(NA,j,j)
       sigmalambda=(t(lambda.supp[,1:j]) %*% lambda.supp[,1:j]) - (N_bal/window)*sigma2*diag(1,j)
       sigmalambda1=(t(lambda.supp[,1:j]) %*% lambda.supp[,1:j])
       w2 = solve(sigma2*oo + sigmalambda)
       
       aaa=( t(riskpremia.supp[1:j])%*%oo%*%w2%*%sigmalambda%*%w2%*%oo%*%riskpremia.supp[1:j])
       
       
       if(aaa<=0)     { sigmalambda=sigmalambda1}      
       
       w2=matrix(NA,nrow=j, ncol=j)
       
       w2 = solve(sigma2*oo + sigmalambda)
       w22 = lambda.supp[,1:j]  %*% w2 %*% oo %*%riskpremia.supp[1:j]
       #w22[redistribute] = 0
       w_tan = w22/sum(w22)
       #outt_unc_oos[j] = sum(w_tan[!redistribute]*Xhalf[!redistribute])
       
       gL= (w2%*%oo%*%riskpremia.supp[1:j])/as.numeric(sum(w22)) 
       
       rrr_con_is1[j]= X_nownext1[window,]%*%w_tan # - (N_bal/Thalf)*sigma2*totfac2[window,]%*%gL
       rrr_con_is1_vec[,j]=X_nownext1[1:window,]%*%w_tan 
       
       w_mv = w22/as.numeric( matrix(riskpremia.supp[1:j],1,j)%*%t( lambda.supp[,1:j])%*% lambda.supp[,1:j]  %*% w2 %*% oo %*%riskpremia.supp[1:j] )
       rrr_con_is2[j]= X_nownext1[window,]%*%w_mv # - (N_bal/Thalf)*sigma2*totfac2[window,]%*%gL
       rrr_con_is2_vec[,j]=X_nownext1[1:window,]%*%w_mv 
       
       
       rrr_con_is3[j]= factor.supp[window,1:j]%*%oo%*% riskpremia.supp[1:j]/
         (sum(oo%*% riskpremia.supp[1:j]) )# - (N_bal/Thalf)*sigma2*totfac2[window,]%*%gL
       rrr_con_is3_vec[,j]=factor.supp[1:window,1:j]%*%oo%*% riskpremia.supp[1:j]/
         (sum(oo%*% riskpremia.supp[1:j]) )
         
       
       
       rrr_con_is4[j]= factor.supp[window,1:j]%*%oo%*% riskpremia.supp[1:j]/
         (matrix(riskpremia.supp[1:j],1,j)%*%oo%*% riskpremia.supp[1:j] )# - (N_bal/Thalf)*sigma2*totfac2[window,]%*%gL
       rrr_con_is4_vec[,j]=factor.supp[1:window,1:j]%*%oo%*% riskpremia.supp[1:j]/
         as.numeric(matrix(riskpremia.supp[1:j],1,j)%*%oo%*% riskpremia.supp[1:j] )# - (N_bal/Thalf)*sigma2*totfac2[window,]%*%gL
       
       
       
       
     }
     
     # nfac= ts.nfactorhat[i]
     # ooo=solve(var(factor.supp[,1:nfac]))
     # rrr_con_oos3due= factor.supp_oos[1:nfac]%*%ooo%*% riskpremia.supp[1:nfac]/
     #   (sum(ooo%*% riskpremia.supp[1:nfac]) )# - (N_bal/Thalf)*sigma2*totfac2[window,]%*%gL
      
     
     rrr_con_is5= factor.supp[window,]  
     rrr_con_is5_vec=factor.supp[1:window,]  
     
     
     
     #  cond oos unbalanced
     
     
     
     # X_in2=data[index,]-rf.supp%*%t(rep(1,ncol(data))) # i : i+w-1
     # X_next2=data[i+window,]-(rf[i+window]-1)%*%t(rep(1,ncol(data)))
     # X_nownext=rbind(X_in2,X_next2)
     # delete_nownext = apply(X_nownext, 2, function(x){sum(is.na(x))})
     # delete_nownext = which(delete_nownext > 0)
     # 
     # 
     # N_nownext=ncol(X_nownext[,-delete_nownext])
     # # this is from i to i+windows so size window+1
     # X_nownext = X_nownext[,-delete_nownext]
     # #X_nownext = X_nownext -(rf[i:(i+window)]-1)%*%t(rep(1,N_nownext))
     # X_next=X_nownext[window+1,]
      
     
     
     
     lambda.supp=sqrt(N_nownext) * as.matrix(eigen(t(X_nownext[1:window,]) %*% (X_nownext[1:window,]))$vectors[,1:10])
     factor.supp = (X_nownext[1:window,] %*%  lambda.supp)/N_nownext
     riskpremia.supp=matrix(apply(factor.supp,2,mean),10,1)
     omega.supp=var(factor.supp)
     
     
     factor.supp_oos =( X_nownext[(window+1),] %*%  lambda.supp)/N_nownext # i+w
     rrr_con_unbal_oos1=matrix(0,10,1)
     rrr_con_unbal_oos2=matrix(0,10,1)
     rrr_con_unbal_oos3=matrix(0,10,1)
     rrr_con_unbal_oos4=matrix(0,10,1)
     rrr_con_unbal_oos5=matrix(0,10,1)
     
     
     j=1
     for (j in 1:10)
     {  
       oo=solve(omega.supp[1:j,1:j])
       val=0
       val=  (1/(N_nownext*window) ) * sum((X_nownext[1:window,] -  factor.supp[,1:j] %*% t(lambda.supp[,1:j]))^2)
       sigma2=0
       sigma2=(window/(window - j))*val
       
       sigmalambda=matrix(NA,j,j)
       sigmalambda1=matrix(NA,j,j)
       sigmalambda=(t(lambda.supp[,1:j]) %*% lambda.supp[,1:j]) - (N_nownext/window)*sigma2*diag(1,j)
       sigmalambda1=(t(lambda.supp[,1:j]) %*% lambda.supp[,1:j])
       w2 = solve(sigma2*oo + sigmalambda)
       
       aaa=( t(riskpremia.supp[1:j])%*%oo%*%w2%*%sigmalambda%*%w2%*%oo%*%riskpremia.supp[1:j])
       
       
       if(aaa<=0)     { sigmalambda=sigmalambda1}      
       
       w2=matrix(NA,nrow=j, ncol=j)
       
       w2 = solve(sigma2*oo + sigmalambda)
       w22 = lambda.supp[,1:j]  %*% w2 %*% oo %*%riskpremia.supp[1:j]
       #w22[redistribute] = 0
       w_tan = w22/sum(w22)
       #outt_unc_oos[j] = sum(w_tan[!redistribute]*Xhalf[!redistribute])
       
       gL= (w2%*%oo%*%riskpremia.supp[1:j])/as.numeric(sum(w22)) 
       
       rrr_con_unbal_oos1[j]= X_nownext[(window+1),]%*%w_tan # - (N_bal/Thalf)*sigma2*totfac2[window,]%*%gL
       
       w_mv = w22/as.numeric( matrix(riskpremia.supp[1:j],1,j)%*%t( lambda.supp[,1:j])%*% lambda.supp[,1:j]  %*% w2 %*% oo %*%riskpremia.supp[1:j] )
       rrr_con_unbal_oos2[j]= X_nownext[(window+1),]%*%w_mv # - (N_bal/Thalf)*sigma2*totfac2[window,]%*%gL
       
       
       
       rrr_con_unbal_oos3[j]= factor.supp_oos[1:j]%*%oo%*% riskpremia.supp[1:j]/
         (sum(oo%*% riskpremia.supp[1:j]) )# - (N_bal/Thalf)*sigma2*totfac2[window,]%*%gL
       
       
       
       
       rrr_con_unbal_oos4[j]= factor.supp_oos[1:j]%*%oo%*% riskpremia.supp[1:j]/
         (matrix(riskpremia.supp[1:j],1,j)%*%oo%*% riskpremia.supp[1:j] )# - (N_bal/Thalf)*sigma2*totfac2[window,]%*%gL
       
       
       
       
       
     }
     # nfac= ts.nfactorhat[i]
     # ooo=solve(var(factor.supp[,1:nfac]))
     # rrr_con_oos3due= factor.supp_oos[1:nfac]%*%ooo%*% riskpremia.supp[1:nfac]/
     #   (sum(ooo%*% riskpremia.supp[1:nfac]) )# - (N_bal/Thalf)*sigma2*totfac2[window,]%*%gL
     # 
     
     rrr_con_unbal_oos5= factor.supp_oos  
     
     
     

     
     
     # cond is unbalanced

     
     
     
     
     
     # lambda.supp=sqrt(N_nownext) * as.matrix(eigen(t(X_nownext[1:window,]) %*% (X_nownext[1:window,]))$vectors[,1:10])
     # factor.supp = (X_nownext[1:window,] %*%  lambda.supp)/N_nownext
     # riskpremia.supp=matrix(apply(factor.supp,2,mean),10,1)
     # omega.supp=var(factor.supp)
      
     
     factor.supp_oos =factor.supp[window,]
     #( X_nownext[(window),] %*%  lambda.supp)/N_nownext # i+w
     rrr_con_unbal_is1=matrix(0,10,1)
     rrr_con_unbal_is2=matrix(0,10,1)
     rrr_con_unbal_is3=matrix(0,10,1)
     rrr_con_unbal_is4=matrix(0,10,1)
     rrr_con_unbal_is5=matrix(0,10,1)
     
     rrr_con_unbal_is1_vec=matrix(0,window,10)
     rrr_con_unbal_is2_vec=matrix(0,window,10)
     rrr_con_unbal_is3_vec=matrix(0,window,10)
     rrr_con_unbal_is4_vec=matrix(0,window,10)
     rrr_con__unbalis5_vec=matrix(0,window,10)
     
     
     
     j=1
     for (j in 1:10)
     {  
       oo=solve(omega.supp[1:j,1:j])
       val=0
       val=  (1/(N_nownext*window) ) * sum((X_nownext[1:window,] -  factor.supp[,1:j] %*% t(lambda.supp[,1:j]))^2)
       sigma2=0
       sigma2=(window/(window - j))*val
       
       sigmalambda=matrix(NA,j,j)
       sigmalambda1=matrix(NA,j,j)
       sigmalambda=(t(lambda.supp[,1:j]) %*% lambda.supp[,1:j]) - (N_nownext/window)*sigma2*diag(1,j)
       sigmalambda1=(t(lambda.supp[,1:j]) %*% lambda.supp[,1:j])
       w2 = solve(sigma2*oo + sigmalambda)
       
       aaa=( t(riskpremia.supp[1:j])%*%oo%*%w2%*%sigmalambda%*%w2%*%oo%*%riskpremia.supp[1:j])
       
       
       if(aaa<=0)     { sigmalambda=sigmalambda1}      
       
       w2=matrix(NA,nrow=j, ncol=j)
       
       w2 = solve(sigma2*oo + sigmalambda)
       w22 = lambda.supp[,1:j]  %*% w2 %*% oo %*%riskpremia.supp[1:j]
       #w22[redistribute] = 0
       w_tan = w22/sum(w22)
       #outt_unc_oos[j] = sum(w_tan[!redistribute]*Xhalf[!redistribute])
       
       gL= (w2%*%oo%*%riskpremia.supp[1:j])/as.numeric(sum(w22)) 
       
       rrr_con_unbal_is1[j]= X_nownext[window,]%*%w_tan # - (N_bal/Thalf)*sigma2*totfac2[window,]%*%gL
       rrr_con_unbal_is1_vec[,j]=X_nownext[1:window,]%*%w_tan
       
       
       w_mv = w22/as.numeric( matrix(riskpremia.supp[1:j],1,j)%*%t( lambda.supp[,1:j])%*% lambda.supp[,1:j]  %*% w2 %*% oo %*%riskpremia.supp[1:j] )
       rrr_con_unbal_is2[j]= X_nownext[window,]%*%w_mv # - (N_bal/Thalf)*sigma2*totfac2[window,]%*%gL
       rrr_con_unbal_is2_vec[,j]=X_nownext[1:window,]%*%w_mv
       
       
       rrr_con_unbal_is3[j]= factor.supp_oos[1:j]%*%oo%*% riskpremia.supp[1:j]/
         (sum(oo%*% riskpremia.supp[1:j]) )# - (N_bal/Thalf)*sigma2*totfac2[window,]%*%gL
      
        rrr_con_unbal_is3_vec[,j]= factor.supp[,1:j]%*%oo%*% riskpremia.supp[1:j]/
         (sum(oo%*% riskpremia.supp[1:j]) )
       
       
       
       rrr_con_unbal_is4[j]= factor.supp_oos[1:j]%*%oo%*% riskpremia.supp[1:j]/
         (matrix(riskpremia.supp[1:j],1,j)%*%oo%*% riskpremia.supp[1:j] )# - (N_bal/Thalf)*sigma2*totfac2[window,]%*%gL
       rrr_con_unbal_is4_vec[,j]= factor.supp[,1:j]%*%oo%*% riskpremia.supp[1:j]/
         as.numeric(matrix(riskpremia.supp[1:j],1,j)%*%oo%*% riskpremia.supp[1:j] )
       
       
       
       
     }
     # nfac= ts.nfactorhat[i]
     # ooo=solve(var(factor.supp[,1:nfac]))
     # rrr_con_oos3due= factor.supp_oos[1:nfac]%*%ooo%*% riskpremia.supp[1:nfac]/
     #   (sum(ooo%*% riskpremia.supp[1:nfac]) )# - (N_bal/Thalf)*sigma2*totfac2[window,]%*%gL
     # 
     
     rrr_con_unbal_is5= factor.supp_oos  
     rrr_con_unbal_is5_vec= factor.supp  
     
     
     
     
     
    
    #  oos+1
    
    # ooo+window  OLD VERSION  - NOT SURE OF IT
    
   
    
    totfac2=rbind(as.matrix(Fbal2v[2:window,,(i+1)],window-1,10),t(fhalfcon2v[,(i+1)]))
    totfac2=matrix(totfac2,window,nfac)
    lam.supp=t(X_nownext_bal[1:window,])%*%totfac2%*%solve(t(totfac2)%*%totfac2) 
    #Z=lam.supp%*%apply(totfac2,2,mean)
    
    #X_nownext = X_nownext -(rf[i:(i+window)]-1)%*%t(rep(1,N_nownext))
    #X_next=X_nownext[window+1,]
    omega=var(totfac2)
    risk.supp=apply(totfac2,2,mean)
    
    
    
    
    val=0
    val = 1/(N_bal*window) * sum((X_nownext_bal[1:window,] -   totfac2 %*% t(lam.supp))^2)
    sigma2=0
    sigma2=(window/(window - nfac))*val
    sigmalambda=matrix(NA,nfac,nfac)
    sigmalambda1=matrix(NA,nfac,nfac)
    sigmalambda=(t(lam.supp) %*% lam.supp) - (N_bal/window)*sigma2*diag(1,nfac)
    sigmalambda1=(t(lam.supp) %*% lam.supp)
    w2 = solve(sigma2*solve(omega) + sigmalambda)
    
    aaa=( t(risk.supp)%*%solve(omega)%*%w2%*%sigmalambda%*%w2%*%solve(omega)%*%risk.supp)
    
    
    if(aaa<=0)     { sigmalambda=sigmalambda1}      
    
    w2=matrix(NA,nrow=nfac, ncol=nfac)
    
    w2 = solve(sigma2*solve(omega) + sigmalambda)
    w22 =  lam.supp %*% w2 %*% solve(omega)%*%risk.supp
    #w22[redistribute] = 0
    w_tan = w22/sum(w22)
    #outt_unc_oos[j] = sum(w_tan[!redistribute]*Xhalf[!redistribute])
    
    gL= (w2%*%solve(omega)%*%risk.supp)/as.numeric(sum(w22)) 
    
    rrr_con_oos= X_nownext_bal[(window+1),]%*%w_tan - (N_bal/window)*sigma2*totfac2[window,]%*%gL
    
    rrr_con_oos22=X_nownext_bal[(window+1),]%*%w_tan
    
    # target vol
    if (nfac==1)
    { 
      volp=sqrt( t(risk.supp)%*%solve(omega[1,1])%*%w2%*%t(lam.supp)%*%
                   (omega[1,1]*lam.supp%*%t(lam.supp) +sigma2*diag(N_bal))%*%lam.supp%*%
                   w2%*%solve(omega[1,1])%*%risk.supp)
    } else { volp=sqrt( t(risk.supp)%*%solve(omega)%*%w2%*%t(lam.supp)%*%
                          (lam.supp%*%omega%*%t(lam.supp) +sigma2*diag(N_bal))%*%lam.supp%*%
                          w2%*%solve(omega)%*%risk.supp)}
    tar_vol=0.05/sqrt(12)
    w_vol = (tar_vol)*w22%*%solve(volp)
    #outv[,j] = sum(w_vol[!redistribute]*X.next[!redistribute])
    
    gLv= tar_vol*(w2%*%solve(omega)%*%risk.supp)/as.numeric(volp)
    #      as.numeric((sqrt(t(risk_premia.supp[1:nfac])%*%solve(omega)%*%risk_premia.supp[1:nfac]))) 
    
    rrr_conv_oos=   X_nownext_bal[(window+1),]%*%w_vol - (N_bal/window)*sigma2*totfac2[window,]%*%gLv
    
    rrr_conv_oos2=  X_nownext_bal[(window+1),]%*%w_vol 
    
    #  }
    
    # con unbal e unbal2
    
    
    totfac22=rbind(as.matrix(Fabal2unbalv[2:window,,(i+1)],window-1,10),t(fhalfcon2unbalv[,(i+1)]))
    totfac22=matrix(totfac22,window,nfac)
    lam.supp22=t(X_nownext[1:window,])%*%totfac22%*%solve(t(totfac22)%*%totfac22)
    lam.supp2=t(X_nownext[1:window,])%*%ts.factors[,1:nfac,i]%*%solve(t(ts.factors[,1:nfac,i])%*%ts.factors[,1:nfac,i]) 
    
    
    
    # unbal
    
    omega=var(totfac22)
    risk.supp=apply(totfac22,2,mean)
    
    
    
    
    val=0
    val = 1/(N_nownext*window) * sum((X_nownext[1:window,] -   totfac22 %*% t(lam.supp22))^2)
    sigma2=0
    sigma2=(window/(window - nfac))*val
    sigmalambda=matrix(NA,nfac,nfac)
    sigmalambda1=matrix(NA,nfac,nfac)
    sigmalambda=(t(lam.supp22) %*% lam.supp22) - (N_nownext/window)*sigma2*diag(1,nfac)
    sigmalambda1=(t(lam.supp22) %*% lam.supp22)
    w2 = solve(sigma2*solve(omega) + sigmalambda)
    
    aaa=( t(risk.supp)%*%solve(omega)%*%w2%*%sigmalambda%*%w2%*%solve(omega)%*%risk.supp)
    
    
    if(aaa<=0)     { sigmalambda=sigmalambda1}      
    
    w2=matrix(NA,nrow=nfac, ncol=nfac)
    
    w2 = solve(sigma2*solve(omega) + sigmalambda)
    w22 =  lam.supp22 %*% w2 %*% solve(omega)%*%risk.supp
    #w22[redistribute] = 0
    w_tan = w22/sum(w22)
    #outt_unc_oos[j] = sum(w_tan[!redistribute]*Xhalf[!redistribute])
    
    gL= (w2%*%solve(omega)%*%risk.supp)/as.numeric(sum(w22)) 
    
    rrr_con_unbal_oos= X_nownext[(window+1),]%*%w_tan - (N_nownext/window)*sigma2*totfac22[window,]%*%gL
    
    rrr_con_unbal_oos2=X_nownext[(window+1),]%*%w_tan
    
    # target vol
    if (nfac==1)
    { 
      volp=sqrt( t(risk.supp)%*%solve(omega[1,1])%*%w2%*%t(lam.supp22)%*%
                   (omega[1,1]*lam.supp22%*%t(lam.supp22) +sigma2*diag(N_nownext))%*%lam.supp22%*%
                   w2%*%solve(omega[1,1])%*%risk.supp)
    } else { volp=sqrt( t(risk.supp)%*%solve(omega)%*%w2%*%t(lam.supp22)%*%
                          (lam.supp22%*%omega%*%t(lam.supp22) +sigma2*diag(N_nownext))%*%lam.supp22%*%
                          w2%*%solve(omega)%*%risk.supp)}
    tar_vol=0.05/sqrt(12)
    w_vol = (tar_vol)*w22%*%solve(volp)
    #outv[,j] = sum(w_vol[!redistribute]*X.next[!redistribute])
    
    gLv= tar_vol*(w2%*%solve(omega)%*%risk.supp)/as.numeric(volp)
    #      as.numeric((sqrt(t(risk_premia.supp[1:nfac])%*%solve(omega)%*%risk_premia.supp[1:nfac]))) 
    
    rrr_conv_unbal_oos=   X_nownext[(window+1),]%*%w_vol - (N_nownext/window)*sigma2*totfac22[window,]%*%gLv
    
    rrr_conv_unbal_oos2=  X_nownext[(window+1),]%*%w_vol 
    
    
    # unbal2
    
    
    
    omega=var(ts.factors[,1:nfac,i])
    if (nfac==1)
    {   risk.supp= mean(ts.factors[,1:nfac,i]) } else
    {      risk.supp=apply(ts.factors[,1:nfac,i],2,mean) }
    
    
    #lam.supp2=t(X_nownext[1:window,])%*%ts.factors[,1:nfac,i]/N_nownext 
    
    
    val=0
    val = 1/(N_nownext*window) * sum((X_nownext[1:window,] -   ts.factors[,1:nfac,i]%*% t(lam.supp2))^2)
    sigma2=0
    sigma2=window/(window - nfac)*val
    sigmalambda=matrix(NA,nfac,nfac)
    sigmalambda1=matrix(NA,nfac,nfac)
    sigmalambda=(t(lam.supp2) %*% lam.supp2) - (N_nownext/window)*sigma2*diag(1,nfac)
    sigmalambda1=(t(lam.supp2) %*% lam.supp2)
    w2 = solve(sigma2*solve(omega) + sigmalambda)
    
    aaa=( t(risk.supp)%*%solve(omega)%*%w2%*%sigmalambda%*%w2%*%solve(omega)%*%risk.supp)
    
    
    if(aaa<=0)     { sigmalambda=sigmalambda1}      
    
    w2=matrix(NA,nrow=nfac, ncol=nfac)
    
    w2 = solve(sigma2*solve(omega) + sigmalambda)
    w22 =  lam.supp2 %*% w2 %*% solve(omega)%*%risk.supp
    #w22[redistribute] = 0
    w_tan = w22/sum(w22)
    #outt_unc_oos[j] = sum(w_tan[!redistribute]*Xhalf[!redistribute])
    
    gL= (w2%*%solve(omega)%*%risk.supp)/as.numeric(sum(w22)) 
    
    rrr_con_unbal2_oos= X_nownext[(window+1),]%*%w_tan - (N_nownext/window)*sigma2*ts.factors[window,1:nfac,i]%*%gL
    
    rrr_con_unbal2_oos2=X_nownext[(window+1),]%*%w_tan
    
    # target vol
    if (nfac==1)
    { 
      volp=sqrt( t(risk.supp)%*%solve(omega)%*%w2%*%t(lam.supp2)%*%
                   (omega*lam.supp2%*%t(lam.supp2) +sigma2*diag(N_nownext))%*%lam.supp2%*%
                   w2%*%solve(omega)%*%risk.supp)
    } else { volp=sqrt( t(risk.supp)%*%solve(omega)%*%w2%*%t(lam.supp2)%*%
                          (lam.supp2%*%omega%*%t(lam.supp2) +sigma2*diag(N_nownext))%*%lam.supp2%*%
                          w2%*%solve(omega)%*%risk.supp)}
    tar_vol=0.05/sqrt(12)
    w_vol = (tar_vol)*w22%*%solve(volp)
    #outv[,j] = sum(w_vol[!redistribute]*X.next[!redistribute])
    
    gLv= tar_vol*(w2%*%solve(omega)%*%risk.supp)/as.numeric(volp)
    #      as.numeric((sqrt(t(risk_premia.supp[1:nfac])%*%solve(omega)%*%risk_premia.supp[1:nfac]))) 
    
    rrr_conv_unbal2_oos=   X_nownext[(window+1),]%*%w_vol - (N_nownext/window)*sigma2*ts.factors[window,1:nfac,i]%*%gLv
    
    rrr_conv_unbal2_oos2=  X_nownext[(window+1),]%*%w_vol 
    
    
    
    
    
    
    list(rrr_con_oos,rrr_con_oos22,rrr_conv_oos,rrr_conv_oos2,rrr_con_unbal_oos,rrr_con_unbal_oos2,rrr_conv_unbal_oos,rrr_conv_unbal_oos2,#1-8
         rrr_con_unbal2_oos,rrr_con_unbal2_oos2,rrr_conv_unbal2_oos,rrr_conv_unbal2_oos2,#9-12
         rrr_unc_oos,rrr_unc_oos2,rrr_unc_oos3,rrr_unc_oos4,rrr_unc_oos5,#13-17
         rrr_con_oos1,rrr_con_oos2,rrr_con_oos3,rrr_con_oos4,rrr_con_oos5, rrr_con_oos3due,#18-23
         rrr_con_is1,rrr_con_is2,rrr_con_is3,rrr_con_is4,rrr_con_is5,#24-28
         rrr_con_unbal_oos1,rrr_con_unbal_oos2,rrr_con_unbal_oos3,rrr_con_unbal_oos4,rrr_con_unbal_oos5,#29-33
         rrr_con_unbal_is1,rrr_con_unbal_is2,rrr_con_unbal_is3,rrr_con_unbal_is4,rrr_con_unbal_is5,#34-38
         rrr_unc_is1,rrr_unc_is2,rrr_unc_is3,rrr_unc_is4,rrr_unc_is5,#39-43
         rrr_bal_1N,rrr_unb_1N,#44-45
         rrr_unc_is1_vec, rrr_unc_is2_vec,rrr_unc_is3_vec,rrr_unc_is4_vec,rrr_unc_is5_vec,#46-50
         rrr_con_is1_vec, rrr_con_is2_vec,rrr_con_is3_vec,rrr_con_is4_vec,rrr_con_is5_vec,#51-55
         rrr_con_unbal_is1_vec,rrr_con_unbal_is2_vec,rrr_con_unbal_is3_vec,rrr_con_unbal_is4_vec,rrr_con_unbal_is5_vec#56-60
         
         
         
         )
         

   
    
         
         

   
   
    
  
    
    
    
    
    
    
  
  }
  
  
  clusterExport(cl, c("criteria", "V", "V_exp","vec","sqrtm","tr","solve","dnorm","qnorm","kronecker",
                      "SDF_fun","SDFu_fun","SDF_vol","SDF_volFF","SDFu_vol","SDF_corr","SDFFF_corr","SDF_acm_fun", "RiskPremia_fun", "RiskPremia_acm_fun", "commutation_matrix"))
  pardef = par()
  
}



# input for Unconditional

data_type = "single_assets"  # single_assets or portfolio

window = 480
kmax = 10
nk = kmax
eps1 =0 # 0.12 # 1e-8
eps2 =  0.06
# window  12      24      36      60      120       648
# eps2    0.175   0.11    0.06    0.015   0.0001   0.0001    for single assets
# eps2    0.001   ...     1e-5    ...     ...      for portfolios (al diminuire non cambia)

# DATA
{
  # IMPORTING data and creating RECESSION variable
  # Creazione recession variable
  {
    rec = read.csv("01_Data/NBER_recession_Alberto.csv", stringsAsFactors = F)
    rec[,1] = as.Date(rec[,1], format = "%Y-%m-%d")
    rec1 = seq.Date(rec[nrow(rec),1], Sys.Date(), by = "month")[-1]
    rec[(nrow(rec) + 1):(nrow(rec) + length(rec1)), ] = data.frame(rec1, rep(NA, length(rec1)))
    rec2 = matrix(c(#1977, 08, 1977, 11,
      1982, 08, 1982, 12,
      1986, 10, 1986, 12,
      1987, 10, 1987, 12,
      1989, 10, 1989, 12,
      1991, 10, 1991, 12,
      1997, 06, 1997, 08,
      1998, 08, 1998, 10,
      2000, 02, 2000, 04,
      2005, 08, 2005, 11,
      2010, 08, 2010, 09,
      2012, 05, 2012, 07), ncol = 4, byrow = T)
    
    rec3 = matrix(NA, ncol = 2, nrow = nrow(rec2))
    for (i in 1:nrow(rec2))
    {
      rec3[i,1] = which(as.numeric(rec[,1]) == as.numeric(as.Date(paste(01, "-", rec2[i,2], "-",rec2[i,1], sep = ""), format = "%d-%m-%Y")))
      rec3[i,2] = which(c(rec[,1]) == as.numeric(as.Date(paste(01, "-", rec2[i,4], "-",rec2[i,3], sep = ""), format = "%d-%m-%Y")))
    }
    for (i in 1:nrow(rec2)) rec[rec3[i,1]:rec3[i,2],2] = 1
    rec[is.na(rec[,2]),2] = 0
    
    rm(rec1, rec2, rec3, i)
  }
  # Importazione dati (e aggiustamento variabile recessione)
  {
    if (data_type == "single_assets")
    {
      data = read.table("01_Data/dataAlberto198001201912.txt", header = FALSE)
      data = as.matrix(data)
      t = read.table("01_Data/datesAlberto198001201912.txt", header = FALSE)
      t = as.vector(as.matrix(t))
      #t = as.numeric(data[1,])
      #data = data[-1,]
      for (i in 1:length(t))
      {
        data[i,is.nan(data[i,])] = NA
        
        m = substr(t[i], 5, 6)
        d = "01" #substr(t[i], 5, 6)
        y = substr(t[i], 1, 4)
        t[i] = paste(d, "-", m, "-", y, sep = "")
      }
      t = as.Date(t, format = "%d-%m-%Y")
      #data = t(as.matrix(data))  # to make data TxN. already monthly and in decimals
      rownames(data) = t
    }
    
    if (data_type == "portfolio")
    {
      data = read.csv("01_Data/portfolios_t.csv", header=TRUE, skip = 2)
      t = as.Date(data[,1], format = "%d/%m/%Y")
      data = read.csv("01_Data/portfolios_data.csv", header=TRUE)
      rownames(data) = t
      data = data/100 # data were in percentage, already monthly
    }
    
    id = which(rec[,1] == t[1]):which(rec[,1] == t[length(t)])
    rec = rec[id,]
    rec[rec[,2] == 1, 2] =  rec[rec[,2] == 1, 1]
    rec = rec[,-1]
    
    nw = length(t) - window + 1
    rm(id)
  }
  # Importazione FF-factors and risk-free
  {
    FF3 = read.table("01_Data/F-F_Research_Data_Factors.txt", header = T, skip = 3)
    FF5 = read.table("01_Data/F-F_Research_Data_5_Factors_2x3.txt", header = T, skip = 3)
    FF5 = rbind(matrix(NA, nrow = nrow(FF3) - nrow(FF5), ncol = ncol(FF5)), as.matrix(FF5))
    rf = FF3[,4]/100 # it was in percentage, already monthly basis
    
    start_supp = paste(substr(t[1], 1, 4), substr(t[1], 6, 7), sep = "")
    end_supp = paste(substr(t[length(t)], 1, 4), substr(t[length(t)], 6, 7), sep = "")
    
    i_supp = which(rownames(FF3) == start_supp):which(rownames(FF3) == end_supp)
    FF3 = FF3[i_supp,]
    FF3 = as.matrix(FF3[,-4])
    FF5 = FF5[i_supp,]
    FF5 = as.matrix(FF5[,-6])
    FF = FF5 #cbind(FF3, FF5[,4:5])
    
    rf = 1 + rf[i_supp]
    
    rm(start_supp, end_supp, i_supp)
  }
  # Importazione altri dati di mercato
  {
    t_supp = seq.Date(as.Date("01-01-1980", format = "%d-%m-%Y"), as.Date("01-12-2019", format = "%d-%m-%Y"), by = "month")
    if (t[1] < t_supp[1]) 
    {
      missing_start = which(t == t_supp[1]) - 1
      id_start = 1
    }
    if (t[1] >= t_supp[1])
    {
      missing_start = 0
      id_start = which(t_supp == t[1])
    }
    missing_end = 0
    if (t[length(t)] > t_supp[length(t_supp)]) missing_end = length(t) - which(t == t_supp[length(t_supp)])
    
    dy = read.csv("01_Data/divyield_final.csv", header = F, stringsAsFactors = F)
    dy = dy[,1]
    dy = c(rep(NA, missing_start), dy, rep(NA, missing_end))
    dy = dy[id_start:(id_start + length(t) - 1)]
    dy = dy[-(1:(window - 1))]
    
    defsp = read.csv("01_Data/moody_final.csv", header = F, stringsAsFactors = F)
    defsp = defsp[,1]/1200
    defsp = c(rep(NA, missing_start), defsp, rep(NA, missing_end))
    defsp = defsp[id_start:(id_start + length(t) - 1)]
    defsp = defsp[-(1:(window - 1))]
    
    termsp = read.csv("01_Data/termspread_final.csv", header = F, stringsAsFactors = F)
    termsp = termsp[,1]/1200
    termsp = c(rep(NA, missing_start), termsp, rep(NA, missing_end))
    termsp = termsp[id_start:(id_start + length(t) - 1)]
    termsp = termsp[-(1:(window - 1))]
    
    cape = read.csv("01_Data/cape_final.csv", header = F, stringsAsFactors = F)
    cape = cape[,1]/1000
    cape = c(rep(NA, missing_start), cape, rep(NA, missing_end))
    cape = cape[id_start:(id_start + length(t) - 1)]
    cape = cape[-(1:(window - 1))]
    
    SP = read.csv("01_Data/SP500_final.csv", header = F, sep = ";", stringsAsFactors = F)
    SP = SP[,1]
    SP = c(rep(NA, missing_start), SP, rep(NA, missing_end))
    SP = SP[id_start:(id_start + length(t) - 1)]
    SP = SP[-(1:(window - 1))]
    SP = SP/SP[1]
    
    rm(t_supp, missing_end, missing_start, id_start)
  }
}
# Stima NUMERO FATTORI secondo vari criteri
{
  start = end = N.vec = rep(NA, nw)
  PC.p1 = PC.p2 = PC.p3 = rep(NA, nw)
  IC.p1 = IC.p2 = IC.p3 = rep(NA, nw)
  AIC.1 = AIC.2 = AIC.3 = rep(NA, nw)
  BIC.1 = BIC.2 = BIC.3 = rep(NA, nw)
  table = data.frame(start, end, N.vec, PC.p1, PC.p2, PC.p3, IC.p1, IC.p2, IC.p3, AIC.1, BIC.1, AIC.2, BIC.2, AIC.3, BIC.3)
  rm(PC.p1, PC.p2, PC.p3, IC.p1, IC.p2, IC.p3, AIC.1, BIC.1, AIC.2, BIC.2, AIC.3, BIC.3)
  rm(start, end)
  NEW.1 = NEW.2 = NEW.3 = NEW.4 = rep(NA, nw)
  NoPen = rep(NA, nw)
  table = data.frame(table, NEW.1, NEW.2, NEW.3, NEW.4, NoPen)
  rm(NEW.1, NEW.2, NEW.3, NEW.4, NoPen)
  
  time = Sys.time()
  supp = parLapply(cl, 1:nw, f, data = data, rf.tot = rf, window = window, ncol = ncol(table), kmax = kmax, eps1 = eps1, eps2 = eps2, std = T)
  print(Sys.time() - time)
  ts.deletestocks = list()
  for (j in 1:nw) 
  {
    table[j,] = supp[[j]][[1]]
    ts.deletestocks[[j]] = supp[[j]][[2]]
  }
  if (length(ts.deletestocks) == 0) ts.deletestocks = NA
  
  plot(x = t[(1 + window -1):length(t)], y = table[,4], type = "l", main = "", xlab = "timestep", ylab = "n. factors estimated", ylim = c(0, kmax))
  lines(x = t[(1 + window -1):length(t)], y = table[,5], col = 2)
  lines(x = t[(1 + window -1):length(t)], y = table[,7], col = 3)
  lines(x = t[(1 + window -1):length(t)], y = table[,8], col = 4)
  abline(v = rec[which(rec > 0)], col = "gray90")
  legend("bottomleft", legend = colnames(table)[c(4, 5, 7, 8)], col = 1:4, lty = 1)
  
  plot(x = t[(1 + window -1):length(t)], y = table[,16], type = "l", main = "", xlab = "timestep", ylab = "n. factors estimated", ylim = c(0, kmax))
  abline(v = rec[which(rec > 0)], col = "gray90")
  lines(x = t[(1 + window -1):length(t)], y = table[,16])
  legend("topleft", legend = colnames(table)[16], col = 1, lty = 1)
  
  rm(j, supp)
}
selected_nfac = 16
# Creazione serie storiche delle quantita'
{
  time = Sys.time()
  suppu = parLapply(cl, 1:nw, f, data = data, rf.tot = rf, window = window, kmax = kmax, std = F, extract = T, ts.deletestocks = ts.deletestocks)
  print(Sys.time() - time)
  rm(time)
  
  temp = factor_time_series(suppu)
  ts.nstocksu = temp[[1]]
  ts.enddateu = temp[[2]]
  ts.enddateu = t[window:length(t)]
  ts.valuefu = temp[[3]]
  ts.eigenvu = temp[[4]]
  ts.loadmeanu = temp[[5]]
  ts.loadvaru = temp[[6]]
  ts.factorsu = temp[[7]]
  ts.nfactorhatu = as.numeric(table[,selected_nfac])
  ts.sigma4u = temp[[8]]
  ts.gamma2passu = temp[[9]]
#  rm(temp, suppu, selected_nfac)
  rm(temp, supp, selected_nfac); gc()
}




# input for conditional

data_type = "single_assets"  # single_assets or portfolio

window =24 # 24 #120
kmax = 10
nk = kmax
eps1 =1e-8
eps2 =0.1#-0.65#0.0001  #  T=24 0.1
#eps1 =0 # 0.12
#eps2 = -0.4 # forse provare 0.415 #-0.43#-0.375
# bigger the eps, the larger the g(), the smaller the khat
# window  12      24      36      60      120       648
# eps2    0.175   0.11    0.06    0.015   0.0001   0.0001    for single assets
# eps2    0.001   ...     1e-5    ...     ...      for portfolios (al diminuire non cambia)
# 

# DATA
{
  # IMPORTING data and creating RECESSION variable
  # Creazione recession variable
  {
    rec = read.csv("01_Data/NBER_recession_Alberto.csv", stringsAsFactors = F)
    rec[,1] = as.Date(rec[,1], format = "%Y-%m-%d")
    rec1 = seq.Date(rec[nrow(rec),1], Sys.Date(), by = "month")[-1]
    rec[(nrow(rec) + 1):(nrow(rec) + length(rec1)), ] = data.frame(rec1, rep(NA, length(rec1)))
    rec2 = matrix(c(#1977, 08, 1977, 11,
      1982, 08, 1982, 12,
      1986, 10, 1986, 12,
      1987, 10, 1987, 12,
      1989, 10, 1989, 12,
      1991, 10, 1991, 12,
      1997, 06, 1997, 08,
      1998, 08, 1998, 10,
      2000, 02, 2000, 04,
      2005, 08, 2005, 11,
      2010, 08, 2010, 09,
      2012, 05, 2012, 07), ncol = 4, byrow = T)
    
    rec3 = matrix(NA, ncol = 2, nrow = nrow(rec2))
    for (i in 1:nrow(rec2))
    {
      rec3[i,1] = which(as.numeric(rec[,1]) == as.numeric(as.Date(paste(01, "-", rec2[i,2], "-",rec2[i,1], sep = ""), format = "%d-%m-%Y")))
      rec3[i,2] = which(c(rec[,1]) == as.numeric(as.Date(paste(01, "-", rec2[i,4], "-",rec2[i,3], sep = ""), format = "%d-%m-%Y")))
    }
    for (i in 1:nrow(rec2)) rec[rec3[i,1]:rec3[i,2],2] = 1
    rec[is.na(rec[,2]),2] = 0
    
    rm(rec1, rec2, rec3, i)
  }
  # Importazione dati (e aggiustamento variabile recessione)
  {
    if (data_type == "single_assets")
    {
      data = read.table("01_Data/dataAlberto198001201912.txt", header = FALSE)
      data = as.matrix(data)
      t = read.table("01_Data/datesAlberto198001201912.txt", header = FALSE)
      t = as.vector(as.matrix(t))
      #t = as.numeric(data[1,])
      #data = data[-1,]
      for (i in 1:length(t))
      {
        data[i,is.nan(data[i,])] = NA
        
        m = substr(t[i], 5, 6)
        d = "01" #substr(t[i], 5, 6)
        y = substr(t[i], 1, 4)
        t[i] = paste(d, "-", m, "-", y, sep = "")
      }
      t = as.Date(t, format = "%d-%m-%Y")
      #data = t(as.matrix(data))  # to make data TxN. already monthly and in decimals
      rownames(data) = t
    }
    
    if (data_type == "portfolio")
    {
      data = read.csv("01_Data/portfolios_t.csv", header=TRUE, skip = 2)
      t = as.Date(data[,1], format = "%d/%m/%Y")
      data = read.csv("01_Data/portfolios_data.csv", header=TRUE)
      rownames(data) = t
      data = data/100 # data were in percentage, already monthly
    }
    
    id = which(rec[,1] == t[1]):which(rec[,1] == t[length(t)])
    rec = rec[id,]
    rec[rec[,2] == 1, 2] =  rec[rec[,2] == 1, 1]
    rec = rec[,-1]
    
    nw = length(t) - window + 1
    rm(id)
  }
  # Importazione FF-factors and risk-free
  {
    FF3 = read.table("01_Data/F-F_Research_Data_Factors.txt", header = T, skip = 3)
    FF5 = read.table("01_Data/F-F_Research_Data_5_Factors_2x3.txt", header = T, skip = 3)
    FF5 = rbind(matrix(NA, nrow = nrow(FF3) - nrow(FF5), ncol = ncol(FF5)), as.matrix(FF5))
    rf = FF3[,4]/100 # it was in percentage, already monthly basis
    
    start_supp = paste(substr(t[1], 1, 4), substr(t[1], 6, 7), sep = "")
    end_supp = paste(substr(t[length(t)], 1, 4), substr(t[length(t)], 6, 7), sep = "")
    
    i_supp = which(rownames(FF3) == start_supp):which(rownames(FF3) == end_supp)
    FF3 = FF3[i_supp,]
    FF3 = as.matrix(FF3[,-4])
    FF5 = FF5[i_supp,]
    FF5 = as.matrix(FF5[,-6])
    FF = FF5 #cbind(FF3, FF5[,4:5])
    
    rf = 1 + rf[i_supp]
    
    rm(start_supp, end_supp, i_supp)
  }
  # Importazione altri dati di mercato
  {
    t_supp = seq.Date(as.Date("01-01-1980", format = "%d-%m-%Y"), as.Date("01-12-2019", format = "%d-%m-%Y"), by = "month")
    if (t[1] < t_supp[1]) 
    {
      missing_start = which(t == t_supp[1]) - 1
      id_start = 1
    }
    if (t[1] >= t_supp[1])
    {
      missing_start = 0
      id_start = which(t_supp == t[1])
    }
    missing_end = 0
    if (t[length(t)] > t_supp[length(t_supp)]) missing_end = length(t) - which(t == t_supp[length(t_supp)])
    
    dy = read.csv("01_Data/divyield_final.csv", header = F, stringsAsFactors = F)
    dy = dy[,1]
    dy = c(rep(NA, missing_start), dy, rep(NA, missing_end))
    dy = dy[id_start:(id_start + length(t) - 1)]
    dy = dy[-(1:(window - 1))]
    
    defsp = read.csv("01_Data/moody_final.csv", header = F, stringsAsFactors = F)
    defsp = defsp[,1]/1200
    defsp = c(rep(NA, missing_start), defsp, rep(NA, missing_end))
    defsp = defsp[id_start:(id_start + length(t) - 1)]
    defsp = defsp[-(1:(window - 1))]
    
    termsp = read.csv("01_Data/termspread_final.csv", header = F, stringsAsFactors = F)
    termsp = termsp[,1]/1200
    termsp = c(rep(NA, missing_start), termsp, rep(NA, missing_end))
    termsp = termsp[id_start:(id_start + length(t) - 1)]
    termsp = termsp[-(1:(window - 1))]
    
    cape = read.csv("01_Data/cape_final.csv", header = F, stringsAsFactors = F)
    cape = cape[,1]/1000
    cape = c(rep(NA, missing_start), cape, rep(NA, missing_end))
    cape = cape[id_start:(id_start + length(t) - 1)]
    cape = cape[-(1:(window - 1))]
    
    SP = read.csv("01_Data/SP500_final.csv", header = F, sep = ";", stringsAsFactors = F)
    SP = SP[,1]
    SP = c(rep(NA, missing_start), SP, rep(NA, missing_end))
    SP = SP[id_start:(id_start + length(t) - 1)]
    SP = SP[-(1:(window - 1))]
    SP = SP/SP[1]
    
    rm(t_supp, missing_end, missing_start, id_start)
  }
} 


# Stima NUMERO FATTORI secondo vari criteri
{
  start = end = N.vec = rep(NA, nw)
  PC.p1 = PC.p2 = PC.p3 = rep(NA, nw)
  IC.p1 = IC.p2 = IC.p3 = rep(NA, nw)
  AIC.1 = AIC.2 = AIC.3 = rep(NA, nw)
  BIC.1 = BIC.2 = BIC.3 = rep(NA, nw)
  table = data.frame(start, end, N.vec, PC.p1, PC.p2, PC.p3, IC.p1, IC.p2, IC.p3, AIC.1, BIC.1, AIC.2, BIC.2, AIC.3, BIC.3)
  rm(PC.p1, PC.p2, PC.p3, IC.p1, IC.p2, IC.p3, AIC.1, BIC.1, AIC.2, BIC.2, AIC.3, BIC.3)
  rm(start, end)
  NEW.1 = NEW.2 = NEW.3 = NEW.4 = rep(NA, nw)
  NoPen = rep(NA, nw)
  table = data.frame(table, NEW.1, NEW.2, NEW.3, NEW.4, NoPen)
  rm(NEW.1, NEW.2, NEW.3, NEW.4, NoPen)
  
  time = Sys.time()
  supp = parLapply(cl, 1:nw, f, data = data, rf.tot = rf, window = window, ncol = ncol(table), kmax = kmax, eps1 = eps1, eps2 = eps2, std = T)
  print(Sys.time() - time)
  ts.deletestocks = list()
  for (j in 1:nw) 
  {
    table[j,] = supp[[j]][[1]]
    ts.deletestocks[[j]] = supp[[j]][[2]]
  }
  if (length(ts.deletestocks) == 0) ts.deletestocks = NA
  
  plot(x = t[(1 + window -1):length(t)], y = table[,4], type = "l", main = "", xlab = "timestep", ylab = "n. factors estimated", ylim = c(0, kmax))
  lines(x = t[(1 + window -1):length(t)], y = table[,5], col = 2)
  lines(x = t[(1 + window -1):length(t)], y = table[,7], col = 3)
  lines(x = t[(1 + window -1):length(t)], y = table[,8], col = 4)
  abline(v = rec[which(rec > 0)], col = "gray90")
  legend("bottomleft", legend = colnames(table)[c(4, 5, 7, 8)], col = 1:4, lty = 1)
  
  plot(x = t[(1 + window -1):length(t)], y = table[,16], type = "l", main = "", xlab = "timestep", ylab = "n. factors estimated", ylim = c(0, kmax))
  abline(v = rec[which(rec > 0)], col = "gray90")
  lines(x = t[(1 + window -1):length(t)], y = table[,16])
  legend("topleft", legend = colnames(table)[16], col = 1, lty = 1)
  
  rm(j, supp)
}
selected_nfac = 16
# Creazione serie storiche delle quantita'
{
  time = Sys.time()
  supp = parLapply(cl, 1:nw, f2, data = data, rf.tot = rf, window = window, kmax = kmax, std = F, extract = T, ts.deletestocks = ts.deletestocks)
  print(Sys.time() - time)
  rm(time)
  
  temp = factor_time_series(supp)
  ts.nstocks = temp[[1]]
  ts.enddate = temp[[2]]
  ts.enddate = t[window:length(t)]
  ts.valuef = temp[[3]]
  ts.eigenv = temp[[4]]
  ts.loadmean = temp[[5]]
  ts.loadvar = temp[[6]]
  ts.factors = temp[[7]]
  ts.nfactorhat = as.numeric(table[,selected_nfac])
  ts.sigma4 = temp[[8]]
  ts.gamma2pass = temp[[9]]
#  rm(temp, supp, selected_nfac)
  rm(temp, supp, selected_nfac); gc()
}



windowu=480





# SDF and Risk Premia calculation and CI
{ 
  {
sdf = matrix(NA, nrow = window, ncol = nw)  
sdfu = matrix(NA, nrow = windowu, ncol = nw)  
sdfu_vec = array(NA, c(windowu,10,nw))
sdf_var_vec = matrix(NA, nrow = 10, ncol = nw)  
sdfT_vec_up=matrix(NA,10,nw) 
sdfT_vec_do=matrix(NA,10,nw) 

sdfT_up=matrix(NA,1,nw) 
sdfT_do=matrix(NA,1,nw) 



sdf_vol = matrix(NA, nrow = 1, ncol = nw)
sdf_corr = matrix(NA, nrow = 1, ncol = nw)  
sdfu_vol = matrix(NA, nrow = 1, ncol = nw)  

  
sdfT = rep(NA, nw)
sdfT_vec = rep(NA, 10, nw)


sdf_vec=array(NA,c(window,10,nw))#[,,i] = supp[[(i-1)]][[25]]  # uncond: correposponds to i+w-1
sdfT_vec=matrix(NA,10,nw)#[,i] = sdf_vec[window,,i] 




sdfhalfT = matrix(NA,10,nw)
# change november 2022 QQ
sdfhalfTcon = matrix(NA, nrow = 1, ncol = nw)   # cond bal:  correposponds to i+w-1
sdfhalfTcond = matrix(NA, nrow = 1, ncol = nw)   # cond bal:  correposponds to i+w-1
sdfhalfTconunbal = matrix(NA, nrow = 1, ncol = nw)   # cond unbal:  correposponds to i+w-1


sdfhalfTcon_vec = matrix(NA, nrow = 10, ncol = nw)   # cond bal:  correposponds to i+w-1
sdfhalfTcond_vec = matrix(NA, nrow = 10, ncol = nw)   # cond bal:  correposponds to i+w-1
sdfhalfTconunbal_vec = matrix(NA, nrow = 10, ncol = nw)   # cond unbal:  correposponds to i+w-1



fhalfv= matrix(NA, nrow = 10, ncol = nw) 
fhalfcon2v= matrix(NA, nrow = 10, ncol = nw) 
fhalfcon2unbalv= matrix(NA, nrow = 10, ncol = nw) 

Fbal2v= array(NA, c(window,10,nw))  
Fabal2unbalv= array(NA, c(window,10,nw))  



Fbal2_vec_vec=array(NA, c(window,10,nw)) 
fhalfcon2_vec_vec=matrix(NA, nrow = 10, ncol = nw) 
Fbal2_vec2_vec=array(NA, c(window,10,nw)) 
fhalfcon2_vec2_vec=matrix(NA, nrow = 10, ncol = nw) 
Fbal2unbal_vec_vec=array(NA, c(window,10,nw)) 
fhalfcon2unbal_vec_vec=matrix(NA, nrow = 10, ncol = nw) 





sdfTave = rep(NA, nw)

sdfT_var = rep(NA, nw)
risk_premia = matrix(NA, nrow = nk, ncol = nw)
risk_premia_cov = array(NA, dim = c(nk, nk, nw))
risk_premia_var = matrix(NA, nrow = nk, ncol = nw)
risk_premia_2pass = matrix(NA, nrow = nk, ncol = nw)
risk_premia_rot = matrix(NA, nrow = nk, ncol = nw)
risk_premia_cov_rot = array(NA, dim = c(nk, nk, nw))
risk_premia_var_rot = matrix(NA, nrow = nk, ncol = nw)








sdfFF = matrix(NA, nrow = window, ncol = nw)  
sdfuFF = matrix(NA, nrow = windowu, ncol = nw)  
sdfFF_vol = matrix(NA, nrow = 1, ncol = nw)
sdfFF_corr = matrix(NA, nrow = 1, ncol = nw)  
sdfuFF_vol = matrix(NA, nrow = 1, ncol = nw)  


sdfTFF = rep(NA, nw)
sdfT_varFF = rep(NA, nw)
risk_premiaFF = matrix(NA, nrow = nk, ncol = nw)
risk_premia_covFF = array(NA, dim = c(nk, nk, nw))
risk_premia_varFF = matrix(NA, nrow = nk, ncol = nw)

risk_premia_2passFF = matrix(NA, nrow = nk, ncol = nw)


  
er = rep(0, nw)
}
#this is to evaluate unco rp and omega
# including the one for 1/2 sample
  
  
  
{
  NN=ncol(data)
  lambdau.supp= matrix(NA, nrow = NN, ncol = 10)
  lambdau.supphalf=matrix(NA, nrow = NN, ncol = 10)
  halfT= round(0.5*nrow(data))

  #  select balanced based on 1/2 sample
  
  select = apply(data, 2, function(x){sum(is.na(x))})
  
  select = (select == 0)
  
  data_balanced = data[,select]
  N_bal=ncol(data_balanced)
  T_bal=nrow(data_balanced)
  X_balanced=data_balanced- (rf-1)%*%t(rep(1,N_bal))
  p_bal=t(sdfu)%*%X_balanced/windowu
  
  select_bal=select   # QQQ 
  Nbal=ncol(X_balanced)
  
  Fbal=matrix(0,halfT,10)
  Fbal = sqrt(halfT) * as.matrix(eigen(1/(halfT*Nbal) * X_balanced[1:halfT,] %*% t(X_balanced[1:halfT,]))$vectors[,1:10])
  
  FFbal=matrix(0,halfT,5)
  FFbal=FF[1:halfT,]
  
  
  lambdahalf = t(X_balanced[1:halfT,]) %*% Fbal / halfT
  #  this is to construct loadings in T/2 for balanced
  
  gammahalf= matrix(apply(Fbal, 2, mean),ncol=1)
  omegahalf=matrix(cov(t(matrix(Fbal,nrow=10))),ncol=10)
  
  gammaFFave= matrix(apply(FFbal, 2, mean),ncol=1)
  omegaFFave=matrix(cov(t(matrix(FFbal,nrow=10))),ncol=10)
  
 # lambdahalf=lambdahalf, gammahalf = gammahalf, omegahalf =  omegahalf, data_balanced =
  
  
  
    
  for (i in 1:NN)
  {
    X_i_full = data[,i]             # vettore lungo 648 relativo all'asset i
    i_na = is.na(X_i_full)    # posizioni del vettore X_i_full dove ci sono valori mancanti
    X_i = X_i_full[!i_na]               # vettore esclusi i valori mancanti (le osservazioni potrebbero non essere consecutive)
    F_i =  ts.factorsu[!i_na,,1]                # factors ai soli timesteps dove sono presenti osservazioni per X_i
    lambdau.supp[i,] = t(X_i) %*% F_i / length(X_i)
  
    #  this is full using 1/2 sample
    
   #  X_i_fullhalf = data[1:halfT,i]             # vettore lungo 648 relativo all'asset i
   #  i_nahalf = is.na(X_i_fullhalf)    # posizioni del vettore X_i_full dove ci sono valori mancanti
   #  X_ihalf = X_i_fullhalf[!i_nahalf]               # vettore esclusi i valori mancanti (le osservazioni potrebbero non essere consecutive)
   # F_ihalf =  Fbal[!i_nahalf,]  
   #  
  #  temp = sqrt(halfT) * as.matrix(eigen(1/(halfT*N) * X %*% t(X))$vectors[,1:k])
    
    
    # factors ai soli timesteps dove sono presenti osservazioni per X_i
  #  lambdau.supphalf[i,] = t(X_ihalf) %*% F_ihalf / length(X_ihalf)
    
    
    
    
    }
  
  gammak= matrix(apply(ts.factorsu, 2, mean),ncol=1)
  omegak=matrix(cov(t(matrix(ts.factorsu,nrow=10))),ncol=10)
  
}  
  
  
  #this is to evaluate unco rp and omega and lambda for FF5
  {
    NN=ncol(data)
    lambdau.FF= matrix(NA, nrow = NN, ncol = 5)
    for (i in 1:NN)
    {
      X_i_full = data[1:windowu,i]             # vettore lungo 648 relativo all'asset i
      i_na = is.na(X_i_full)    # posizioni del vettore X_i_full dove ci sono valori mancanti
      X_i = X_i_full[!i_na] # vettore esclusi i valori mancanti (le osservazioni potrebbero non essere consecutive)
      TTi=length(X_i)
      F_i=FF
      F_i = F_i[!i_na,]  # ts.factorsu              # factors ai soli timesteps dove sono presenti osservazioni per X_i
      Muno=diag(1,TTi,TTi)- (1/TTi)*matrix(1,TTi,1)%*%matrix(1,1,TTi)
      if (TTi>10)
{      lambdau.FF[i,] =t(X_i)%*%Muno%*%(F_i)%*%solve( t(F_i)%*%Muno%*%F_i)}# sometimes is NA
    }
    
    gammaaFF= matrix(apply(FF[1:windowu,], 2, mean),ncol=1)
    omegaaFF=matrix(cov(FF[1:windowu,]),ncol=5)
    
    #QQQ
    

  
  
  }
  
  
 
  # ingredients rotation
  
  
  lambda.suppbal1= matrix(0, nrow = NN, ncol = 10)
  lambda.suppbalt= matrix(0, nrow = NN, ncol = 10)
  

  
  
  
  
  factorsTrot=matrix(NA,nrow=windowu,ncol=10)
  factorT=matrix(NA,nrow=windowu,ncol=10)
  factorsTrot_Markus=matrix(NA,nrow=windowu,ncol=10)

    
    
  

    time = Sys.time()
    supp = parLapply(cl,2:nw, SDF_RP_parallel, ts.deletestocks=ts.deletestocks, window=window, lambdahalf=lambdahalf, gammahalf = gammahalf, omegahalf =  omegahalf, X_balanced = X_balanced, data=data, rf = rf, ts.nfactorhat = ts.nfactorhat, ts.factors = ts.factors, ts.factorsu = ts.factorsu,  ts.eigenv = ts.eigenv, ts.valuef = ts.valuef, ts.sigma4 = ts.sigma4, gammak = gammak , omegak = omegak , FF= FF , gammaaFF = gammaaFF , omegaaFF = omegaaFF)
        print(Sys.time() - time)  
        
        i=1
  for (i in 1:(nw-1))
  {

  print(i)
    
  # ingredients for rotation  
    
    # constructing rotation
    i=i+1  # do this because when run SDF_RP it has nw-1 elements from 2 to nw.
   
      index = i:(i + window - 1)
      X = data[index, ]
      
      if (!is.na(ts.deletestocks[[1]][1]))  
      {
        delete = ts.deletestocks[[i]]
        if (class(delete) != "NULL") X = X[,-delete]
        if (class(delete) != "NULL") lambdau=lambdau.supp[-delete,]
        if (class(delete) != "NULL") lambdauFF=lambdau.FF[-delete,]
        
        
        
      }
      X = as.matrix(X)
      N = ncol(X)
      rf.supp = rf[index] - 1
      X = X - rf.supp %*% t(rep(1, N))
      
      X_mean = apply(X, 2, mean)
      
      factor.supp = ts.factors[,,i]
      lambda.supp = t(1/window *(t(factor.supp) %*% X))
      if(i==1){lambda.suppbal1[-delete,]=lambda.supp}
      lambda.suppbalt[-delete,]=lambda.supp
      # Pelger approach
      
      suppp=svd((t(factor.supp)%*%ts.factorsu[index,,1]), nu =10, nv = 10)
      HHM=suppp[[2]]%*%t(suppp[[3]])
      factor.supp_rot_Markus=factor.supp%*%(HHM)
     factorsTrot_Markus[i+window-1,]=factor.supp_rot_Markus[window,]
    
    # end of rotation
    
    nfac = ts.nfactorhat[i]
    sdf[,i] = supp[[(i-1)]][[1]]
    sdfu[,i] = supp[[(i-1)]][[2]]
    
    
    sdfT[i] = sdf[window, i] 

    sdfT_var[i] = supp[[(i-1)]][[3]] 
    if (nfac==1){ risk_premia[1:nfac, i] = mean(factor.supp[,1:nfac])} else
{    risk_premia[1:nfac, i] = apply(factor.supp[,1:nfac],2,mean)} #supp[[i]][[4]]
    aa=  supp[[i-1]][[5]]
    risk_premia_cov[1:nfac, 1:nfac, i]=aa[1:nfac,1:nfac]
    if (nfac==1)    {risk_premia_var[1:nfac, i] =aa[1:nfac,1:nfac]   } else
    {risk_premia_var[1:nfac, i] = diag(aa[1:nfac,1:nfac])}
    
    
    risk_premia_rot[, i] = t(HHM)%*%apply(factor.supp,2,mean)
    risk_premia_cov_rot[,,i] = t(HHM)%*%supp[[(i-1)]][[5]]%*%HHM
    risk_premia_var_rot[,i] = diag(t(HHM)%*%supp[[(i-1)]][[5]]%*%HHM)
    
    
    
    sdf_vol[,i] = supp[[(i-1)]][[6]]
    
    
    omega_inv = solve(omegak[1,1])
    rr=rf[i+window-1]
    sdfu_vol[,i] = (t(gammak[1,])%*%omega_inv%*%gammak[1,])/(rr*rr)
    
  
      sdf_corr[,i] = supp[[(i-1)]][[7]]
    
    
    risk_premia_2pass[, i] = ts.gamma2pass[, nfac, i]
    er[i] = sum(apply(as.matrix(ts.factors[, 1:nfac, i]), 2, mean)*ts.loadmean[1:nfac, i])  
    

    
    
    
    #FF

    sdfFF[,i] = supp[[(i-1)]][[8]]
    sdfuFF[,i] = supp[[(i-1)]][[9]]


    sdfTFF[i] = sdfFF[window, i]
    sdfT_varFF[i] = supp[[(i-1)]][[10]] # zero

    risk_premiaFF[1:5, i] = supp[[(i-1)]][[11]]
    sdfFF_vol[,i] = supp[[(i-1)]][[13]]


    omega_invFF = solve(omegaaFF[1,1])
    rr=rf[i+window-1]
  

    sdfFF_corr[,i] = supp[[(i-1)]][[14]]

    
    sdfhalfT[,i] = supp[[(i-1)]][[15]]  # uncond: correposponds to i+w-1

    sdfhalfTcon[,i] = supp[[(i-1)]][[16]]  # cond bal:  correposponds to i+w-1
    
    sdfhalfTconunbal[,i] = supp[[(i-1)]][[17]]  #  cond unbal: correposponds to i+w-1
    
    fhalfv[,i]= supp[[(i-1)]][[18]]
    fhalfcon2v[,i]= supp[[(i-1)]][[19]]
    fhalfcon2unbalv[,i] = supp[[(i-1)]][[20]]   
    
    
    Fbal2v[,,i]= supp[[(i-1)]][[21]]  
    Fabal2unbalv[,,i]= supp[[(i-1)]][[22]] 
    
    sdfhalfTcon_vec[,i] = supp[[(i-1)]][[23]]  # cond bal:  correposponds to i+w-1
    
    sdfhalfTconunbal_vec[,i] = supp[[(i-1)]][[24]]  #  cond unbal: correposponds to i+w-1
    
    
    
    sdf_vec[,,i] = supp[[(i-1)]][[25]]  # uncond: correposponds to i+w-1
    sdfT_vec[,i] = sdf_vec[window,,i] 
    
    
    
    Fbal2_vec_vec[,,i]=supp[[(i-1)]][[26]] 
    fhalfcon2_vec_vec[,i]=supp[[(i-1)]][[27]]
    Fbal2_vec2_vec[,,i]=supp[[(i-1)]][[28]] 
    fhalfcon2_vec2_vec[,i]=supp[[(i-1)]][[29]]
    Fbal2unbal_vec_vec[,,i]=supp[[(i-1)]][[30]]
    fhalfcon2unbal_vec_vec[,i]=supp[[(i-1)]][[31]]
    sdfu_vec[,,i]=supp[[(i-1)]][[32]]
    sdf_var_vec[,i]=supp[[(i-1)]][[33]]
 
    sdfT_vec_up[,i]=sdfT_vec[,i]+1.96*sqrt(sdf_var_vec[,i]/N)
    sdfT_vec_do[,i]=sdfT_vec[,i]-1.96*sqrt(sdf_var_vec[,i]/N)
    
    sdfT_up[i]=sdfT[i]+1.96*sqrt(sdfT_var[i]/N)
    sdfT_do[i]=sdfT[i]-1.96*sqrt(sdfT_var[i]/N)
    
     
    er[i] = sum(apply(as.matrix(ts.factors[, 1:nfac, i]), 2, mean)*ts.loadmean[1:nfac, i])  
    
    #FF stop
    
      
  }
        
        




# informal plots SDF
{
aa=seq(1:length(pe))
ylim = c(min(pe),max(pe))
plot(x = aa, y = pe, type = "l", main = "Pricing performance PCA", xlab = "Date", ylab = "Pricing Performance", ylim = ylim)
lines(x = aa, y = peu, col='3')

aa=seq(1:length(pe))
ylim = c(min(pe),max(pe))
plot(x = aa, y = pe, type = "l", main = "Pricing performance PCA", xlab = "Date", ylab = "Pricing Performance", ylim = ylim)
lines(x = aa, y = peFF, col='3')

aa=seq(1:length(peu))
ylim = c(min(pe),max(peu))
plot(x = aa, y = peu, type = "l", main = "Pricing performance PCA", xlab = "Date", ylab = "Pricing Performance", ylim = ylim)
lines(x = aa, y = peuFF, col='3')



ylim = c(0,1)
plot(x = ts.enddate, y = sdf_corr, type = "l", main = "Pricing performance PCA", xlab = "Date", ylab = "Pricing Performance", ylim = ylim)



ylim = c(min(sdfT),max(sdfT))
plot(x = ts.enddate, y = sdfT, type = "l", main = "Pricing performance PCA", xlab = "Date", ylab = "Pricing Performance", ylim = ylim)
lines(x =ts.enddate, y = sdfu[window:648,1],col=3)





pe=matrix(pe,ncol=1)
peu=matrix(peu,ncol=1)
uno=seq(1:dim(pe))
ylim = c(min(pe),max(pe))
plot(x = uno, y = pe, type = "l", main = "Pricing performance PCA", xlab = "Date", ylab = "Pricing Performance", ylim = ylim)

 lines(x =uno, y = peu,col=3)

 
 pe=matrix(pe,ncol=1)
 peu=matrix(peu,ncol=1)
 uno=seq(1:361)
 ylim = c(min(pe),max(pe))
 plot(x = uno, y = pe[1:361,], type = "l", main = "Pricing performance PCA", xlab = "Date", ylab = "Pricing Performance", ylim = ylim)
 
 lines(x =uno, y = peFF[1:361],col=3)
 

 
 
 pe=matrix(pe,ncol=1)
 peu=matrix(peu,ncol=1)
 uno=seq(1:361)
 ylim = c(min(pe),max(pe))
 plot(x = uno, y = peu[1:361,], type = "l", main = "Pricing performance PCA", xlab = "Date", ylab = "Pricing Performance", ylim = ylim)
 
 lines(x =uno, y = peuFF[1:361],col=3)
}
 

 

rm(i, j, tot, t)
}
  # Pricing Performance  QQPP
  {
    
    # here all objects needed for is and oos, both bal and unbal. 
    # sdf constructed above and risk premia below.
    # sdf
    #sdfT is the sdf in sample unbal con at time i+win-1
    #sdfu is the sdf in sample bal  uncond at time i+win-1
    #sdfTFF is the sdf in sample unbal FF at time i+win-1
    # sdfhalfT is 10x1  is bal uncond  pseudo oos for i+w-1
        #sdfhalfTcon 1x1  is bal pseudo oos for i+w-1
    #sdfhalfTconunbal 1x1   is unbal pseudo oos for i+w-1
    
    
    
    # risk premia
    
    
    
    #risk_premiau.supp is uncond in sample 10x1
    #risk_premiauFF.supp is FF uncond in sample  5x1
    #risk_premia.supp  is cond in sample  nw x 10
    #riskpremiaFF  nwx5  cond FF
    # risk_premiau_Thalf  is uncon in sample 10x1 using first T/2
    
   # riskpremia_unbal_pseudo  10 x nw-windows  unbal pseudo cond oos i+win  (occhio: diverso da pdf pseudo!)
   # riskpremia_bal_pseudo  10 x nw-windows  bal pseudo cond oos i+win  (occhio: diverso da pdf pseudo!)
    
    
      # nov 24: from here until 5650 - from 6059 to (6231)6342 - do not from 6227 to 7152 (this does 1/2 and 1/2)
    #  nov 24: good one - is from 7178 to 7632 -  oos from 7634 to 8179    QQPP
    
    
    
    time = Sys.time()
    
    k0 = c(1, 3, 5, 7, 10)
    PP.lambda =  PP.lambdau = PP.lambda.nodemean =  PP.lambdau.nodemean =  PP.beta = matrix(NA, nrow = length(k0) + 1, ncol = nw)
    
    PP.lambdaFF =  PP.lambdauFF = PP.lambdaFF.nodemean =  PP.lambdauFF.nodemean  = matrix(NA, nrow =1 , ncol = nw)
  
      PP.lambdau2 =  PP.lambdau3 = PP.lambdau2.nodemean =  PP.lambdau3.nodemean =   matrix(NA, nrow = length(k0) + 1, ncol = nw)
    PP.testdelta = PP.testdelta2=  PP.testdelta_neg = PP.testdelta_pos =   matrix(NA, nrow = length(k0) + 1, ncol = nw)
    hj_cond=hj_uncond=mean_acondFF=mean_auncondFF=mean_condFF=mean_uncondFF=SS_condFF=SS_uncondFF=mean_acond=mean_auncond=mean_cond=mean_uncond=SS_cond=SS_uncond=matrix(NA, nrow = nw, ncol = 1)
    
    PP.h =  PP.hu = PP.h.nodemean =  PP.hu.nodemean =  PP.testh = PP.testh_neg = PP.testh_pos= 
      PP.h.nodemeanFF = PP.testhu = PP.testh_negu = PP.testh_posu =      matrix(NA, nrow = length(k0) + 1, ncol = nw)
  
    TT=window 

    
    

  PP.lambda_oos= PP.lambda.nodemean_oos= PP.lambdauFF_oos=PP.lambdaFF_oos= deltauFF.lambda_oos=PP.lambdauFF.nodemean_oos=deltaFF.lambda_oos=PP.lambdaFF.nodemean_oos=matrix(NA, nrow = length(k0) + 1, ncol = nw)
   delta.lambda_oos=deltau.lambda_oos=PP.lambdau.nodemean_oos=PP.lambdau_oos=matrix(NA, nrow = length(k0) + 1, ncol = nw)
   SS_uncondFF_oos=SS_condFF_oos=SS_uncond_oos=SS_cond_oos=pe_cond_temp_oos= pe_uncond_temp_oos=  pe_condFF_temp_oos=toth_oos= tothu_oos= tothFF_oos= PP.h.nodemeanFF_oos= PP.h.nodemean_oos= PP.h_oos=    PP.hu.nodemean_oos=PP.hu_oos=matrix(NA, nrow = length(k0) + 1, ncol = nw)

   mean_cond_oos= mean_uncond_oos=mean_condFF_oos=mean_uncondFF_oos=mean_acond_oos=mean_auncond_oos=mean_acondFF_oos=mean_auncondFF_oos=matrix(NA, nrow = length(k0) + 1, ncol = nw)
    

     
   

   
   
  
    
   
   
   
   
   
    
    
    

    
    rownames(PP.lambda) = rownames(PP.beta) = c("k = hat(k)", paste("k =", k0))
    colnames(PP.lambda) = colnames(PP.beta) = as.character(ts.enddate)
    
    NN=ncol(data)
    lambdau.supp= matrix(NA, nrow = NN, ncol = 10)
    lambdau.suppFF= matrix(NA, nrow = NN, ncol = 5)
    

    
    for (i in 1:NN)
    {
      X_i_full = data[,i]             # vettore lungo 648 relativo all'asset i
      i_na = is.na(X_i_full)    # posizioni del vettore X_i_full dove ci sono valori mancanti
      X_i = X_i_full[!i_na]               # vettore esclusi i valori mancanti (le osservazioni potrebbero non essere consecutive)
      F_i =  ts.factorsu[!i_na,,1]                # factors ai soli timesteps dove sono presenti osservazioni per X_i
      lambdau.supp[i,] = t(X_i) %*% F_i / length(X_i)
    
    
      X_i_fullFF = data[,i]             # vettore lungo 648 relativo all'asset i
      i_naFF = is.na(X_i_fullFF)    # posizioni del vettore X_i_full dove ci sono valori mancanti
      X_iFF = X_i_fullFF[!i_naFF]               # vettore esclusi i valori mancanti (le osservazioni potrebbero non essere consecutive)
      FFF=FF
      F_iFF =  FFF[!i_naFF,]                # factors ai soli timesteps dove sono presenti osservazioni per X_i
      summ=sum(!i_naFF)
      if (summ<6)
      {lambdau.suppFF[i,] = matrix(NA,1,5)}
      else
{      aa=length(X_iFF)
      MM=(diag(1,aa,aa)-(1/aa)*matrix(1,aa,1)%*%matrix(1,1,aa)  )
      lambdau.suppFF[i,] = t(X_iFF) %*% MM %*% F_iFF %*% solve(t(F_iFF)%*% MM %*% F_iFF)}

    }
  
    
    risk_premiau.supp=  apply(ts.factorsu, 2, mean)
    omegau.supp=cov(matrix(ts.factorsu[,,1],ncol=10))
    
    risk_premiauFF.supp=  apply(FF, 2, mean)
    omegau.supp=cov(FF)
    
    
    i_naLL = is.na(lambdau.suppFF)
    meanlamFF=100*apply(matrix(lambdau.suppFF[!i_naLL],ncol=5), 2, mean)
    
    covlamFF=100*100*cov(matrix(lambdau.suppFF[!i_naLL],ncol=5))
    
   

    
    totX=matrix(0,nrow=NN,ncol=1)
    totLamPrem=matrix(0,nrow=NN,ncol=10)
    totLamPremu=matrix(0,nrow=NN,ncol=10)
    totLamPremFF=matrix(0,nrow=NN,ncol=1)
    totLamPremFFu=matrix(0,nrow=NN,ncol=1)
    
    totdeltaFF=matrix(0,nrow=NN,ncol=1)
    totdeltauFF=matrix(0,nrow=NN,ncol=1)
    totdelta=matrix(0,nrow=NN,ncol=10)
    totdeltau=matrix(0,nrow=NN,ncol=10)
    
    tothFF=matrix(0,nrow=NN,ncol=10)
    toth=matrix(0,nrow=NN,ncol=10)
    tothu=matrix(0,nrow=NN,ncol=10)
    
   
    totX_oos=matrix(0,nrow=NN,ncol=1)
    
    totdeltaFF_oos=matrix(0,nrow=NN,ncol=1)
    totdeltauFF_oos=matrix(0,nrow=NN,ncol=1)
    totdelta_oos=matrix(0,nrow=NN,ncol=10)
    totdeltau_oos=matrix(0,nrow=NN,ncol=10)
    
    tothFF_oos=matrix(0,nrow=NN,ncol=10)
    toth_oos=matrix(0,nrow=NN,ncol=10)
    tothu_oos=matrix(0,nrow=NN,ncol=10)
    
     
    

    
    #   uncond PE
    

    
      select = apply(data, 2, function(x){sum(is.na(x))})
      
      select = (select == 0)
      
      data_balanced = data[,select]
      N_bal=ncol(data_balanced)
      T_bal=nrow(data_balanced)
      X_balanced=data_balanced- (rf-1)%*%t(rep(1,N_bal))
      p_bal=t(sdfu[,2])%*%X_balanced/windowu
      
      select_bal=select   # QQQ 
    
    
    #   uncon risk premia and loadings for T/2

    Thalf=0.5*(nrow(X_balanced))
    factoru_halfT = sqrt(Thalf) * as.matrix(eigen(X_balanced[1:Thalf,] %*% t(X_balanced[1:Thalf,]))$vectors[,1:10])
    riskpremiau_halfT=matrix(apply( factoru_halfT,2,mean),10,1)
    lambdau_halfT = t(1/Thalf *(t(factoru_halfT) %*% X_balanced[1:Thalf,]))
    risk_premiau_Thalf=apply(factoru_halfT,2,mean) #used info up to T/2 and to be used 
    j=1
    sdfu_Thalf=matrix(0,Thalf,10)
    for (j in 1:10)
    {  
      fac_temp=matrix(factoru_halfT[,1:j],nrow=Thalf,ncol=j)
      sdfu_Thalf[,j]=SDF_fun(rf[1:Thalf],j,fac_temp)  }
    
      # this is for the bias                    of the SDF h statistics for unconditional
    
    
    # this is for the bias of the rp delta statistics for unconditional
    
    corr_unc_sdf=matrix(0,10,1)
    corr_unc_rp=matrix(0,10,1)
    
j=1
    for (j in 1:10)
    {
          val_temp = 1/(N_bal*Thalf) * sum((X_balanced[1:Thalf,] -  factoru_halfT[,1:j]%*% t(lambdau_halfT[,1:j]))^2)
          sigma2 = Thalf/(Thalf - j)*val_temp
          aa=matrix(0,Thalf,1)
          aa=(1/Thalf)*sdfu_Thalf[,j]
          corr_unc_sdf[j]= sigma2*t(aa)%*%aa  
          
          corr_unc_rp[j]= (sigma2/Thalf)*(1-t(riskpremiau_halfT[1:j,])%*%riskpremiau_halfT[1:j,]) 
          
          
      }
    
        
    # here am adding the balanced localPCA to be used for oos 
    ts.factor_bal=array(NA,c(window,10,nw))
    ts.factor_unbal=array(NA,c(window,10,nw))
    
    
    
    riskpremia_unbal_pseudo=matrix(NA,10,nw-window)
    riskpremia_bal_pseudo=matrix(NA,10,nw-window)
    
    
    ts.factorsb= array(NA,c(window,10,nw)) 
    ts.sdfb=array(NA,c(window,10,nw))

    
    
    TTT=480
    
    sdfu_vec=matrix(NA,10,TTT)
        j=1
        for (j in 1:10)
        {
    sdfu_vec[j,]=SDF_fun(rf,j,as.matrix(ts.factorsu[,1:j,1],TTT,j))
        }
    
    
    i=2
    for (i in 2:(nw-window))# i take away -window from nw because of rolling oos
    {
      print(i)
    
      
      
      index = i:(i + window-1)
      index_oos = (i+window):(i + 2*window-1)
      indexlag1 = (i-1):(i + window-2)
      
      rf.supp = rf[index] - 1
      rf.oos = rf[index_oos] - 1
      
      X = data[index,]
      X_lag1 = data[indexlag1,]
      
      X_oos=data[index_oos,]
      
    X_balanced_local=X_balanced[index,]
    
    X_balanced_local_lag1=X_balanced[indexlag1,]
    
      
      
    
      X_temp = data[c(indexlag1,index,index_oos),]
      delete_oos = apply(X_temp, 2, function(x){sum(is.na(x))})
      delete_oos = which(delete_oos > 0)
      delete=delete_oos
      
      
      
      
      X = X[,-delete_oos]
      
      N = ncol(X)
      X = as.matrix(X) - rf.supp %*% t(rep(1, N))
      
      
      X_oos = X_oos[,-delete_oos]
      
      N_oos = ncol(X_oos)
      X_oos = as.matrix(X_oos) - rf.oos %*% t(rep(1, N_oos))
      
      
      X_lag1 = X_lag1[,-delete_oos]
      
      N = ncol(X_lag1)
      X_lag1 = as.matrix(X_lag1) - rf.supp %*% t(rep(1, N))
      
      
      # here just create the local balaned PCA
      
      nfac = ts.nfactorhat[i]
      lambda.suppbal =  sqrt(Nbal) * as.matrix(eigen(t(X_balanced_local_lag1) %*% (X_balanced_local_lag1))$vectors[,1:10])
      
      ts.factor_bal[,,i] = (1/Nbal) *(X_balanced_local_lag1 %*% lambda.suppbal)
      
      ts.factorsb[,,i] =  sqrt(window) * as.matrix(eigen((X_balanced_local) %*% t(X_balanced_local))$vectors[,1:10])
      
  
      
       j=1
      for (j in 1:10)
{      ts.sdfb[,j,i]=  SDF_fun(rf[index],j,matrix(ts.factorsb[,1:j,i],window,j))
      } 
       
       
       
    factor_bal_pseudo =(t(X_balanced[i+window-1,])%*%lambda.suppbal)/nrow(lambda.suppbal)
      
       j=1
       for (j in 1:10) 
    {  riskpremia_bal_pseudo[1:j,i]=
   ( ((window-1)/window)*apply(as.matrix(ts.factor_bal[2:window,1:j,i],23,j),2,mean)+
        (1/window)*factor_bal_pseudo[1:j] )
       }
      
      
      
      lambda.suppunbal =  sqrt(N) * as.matrix(eigen(t(X_lag1) %*% (X_lag1))$vectors[,1:10])
      
      ts.factor_unbal[,,i] = (1/N) *(X_lag1 %*% lambda.suppunbal)
      
      
      
      
      factor_unbal_pseudo =t(X[window,]%*%lambda.suppunbal)/nrow(lambda.suppunbal)

     j=1
     for (j in 1:10)
     {
      riskpremia_unbal_pseudo[1:j,i]=
      ( ((window-1)/window)*apply(as.matrix( ts.factor_unbal[2:window,1:j,i],23,10),2,mean)+
            (1/window)*factor_unbal_pseudo[1:j] )
     }
      
      
    }   
      sdfuu=as.matrix(sdfu[index,45])
      sdfuuFF=as.matrix(sdfuFF[index,45])
      
      
      lambdau=lambdau.supp[-delete,]
      lambdauFF=lambdau.suppFF[-delete,]
      
      
  
      X_mean = as.matrix(apply(X, 2, mean)) #from i to i+win-1
      X_mean_oos =as.matrix( apply(X_oos, 2, mean))# from i+win to i+2win-1
      
      factor.supp = ts.factors[,,i]
      lambda.supp = t(1/window *(t(factor.supp) %*% X))
      risk_premia.supp = apply(factor.supp, 2, mean)
      valuef.supp = ts.valuef[, i]
      
      k = c(ts.nfactorhat[i], k0)
      

      
      pe_cond_oos=t(sdf[,i])%*%X_oos/window
      pe_uncond_oos=t(sdfuu)%*%X_oos/window
      pe_condFF_oos=t(sdfFF[,i])%*%X_oos/window
      
      
      
      
      SS_cond_oos[1,i]=(pe_cond_oos)%*%t(pe_cond_oos)/N
      SS_uncond_oos[1,i]=(pe_uncond_oos)%*%t(pe_uncond_oos)/N
      
      SS_condFF_oos[1,i]=(pe_condFF_oos)%*%t(pe_condFF_oos)/N
      
      
      mean_cond_oos[1,i]=mean(pe_cond_oos)
      mean_uncond_oos[1,i]=mean(pe_uncond_oos)
      mean_condFF_oos[1,i]=mean(pe_condFF_oos)
      
      
      
      mean_acond_oos[1,i]=mean(abs(pe_cond_oos))
      mean_auncond_oos[1,i]=mean(abs(pe_uncond_oos))
      mean_acondFF_oos[1,i]=mean(abs(pe_condFF_oos))
 
        
      
      
      
      # end of oos
      
    
    deltauFF.lambda_oos = X_mean_oos - as.matrix(lambdauFF[,1:5]) %*% as.matrix(risk_premiauFF.supp[1:5])
    PP.lambdauFF.nodemean_oos[i] = mean(deltauFF.lambda_oos^2)
    
    
    riskpremiaFF=apply(FF[index,],2,mean)
    
    MM=diag(1,nrow=window)-matrix(1,window,1)%*%matrix(1,1,window)/window
    lambda.suppFF =solve(t(FF[index,])%*%MM%*%FF[index,])%*%t(FF[index,])%*%MM%*%X 
    eFF=X-FF[index,]%*%lambda.suppFF
    sigma2FF=tr(eFF%*%t(eFF))/(N*(window-5-1))
    deltaFF.lambda_oos = X_mean_oos - t(as.matrix(lambda.suppFF)) %*% as.matrix(riskpremiaFF)
    
    PP.lambdaFF.nodemean_oos[i] = mean(deltaFF.lambda_oos^2)
    PP.lambdaFF_oos[i] = mean(deltaFF.lambda_oos^2) - (sigma2FF/window)*(1 +apply(FF[index,],2,mean)%*%solve(cov(FF[index,])) %*%matrix(apply(FF[index,],2,mean),nrow=5,ncol=1))
    PP.lambdauFF_oos[i] = mean(deltauFF.lambda_oos^2) -(sigma2FF/window)*(1 +(risk_premiauFF.supp[1:5])%*%solve(omegau.supp)%*%matrix(risk_premiauFF.supp[1:5],nrow=5,ncol=1))
    
    
    totX_oos[-delete,]=totX_oos[-delete,]+ matrix(X_mean_oos,nrow=N,ncol=1)
    
    
    totdeltaFF_oos[-delete,]=totdeltaFF_oos[-delete,]+ deltaFF.lambda_oos
    totdeltauFF_oos[-delete,]=totdeltauFF_oos[-delete,]+ deltauFF.lambda_oos
    
    
    
    
    j=1
    for (j in 1:length(k))
    {
      
      
      
      
      
      
      delta.lambda_oos = X_mean_oos - as.matrix(lambda.supp[,1:k[j]]) %*% as.matrix(risk_premia.supp[1:k[j]])
      deltau.lambda_oos = X_mean_oos - as.matrix(lambdau[,1:k[j]]) %*% as.matrix(risk_premiau.supp[1:k[j]])
      deltau2.lambda = X_mean - as.matrix(lambda.supp[,1:k[j]]) %*% as.matrix(risk_premiau.supp[1:k[j]])
      deltau3.lambda = X_mean - as.matrix(lambdau[,1:k[j]]) %*% as.matrix(risk_premia.supp[1:k[j]])
      
      
      
      totdelta_oos[-delete,k[j]]=totdelta_oos[-delete,k[j]]+ delta.lambda_oos
      totdeltau_oos[-delete,k[j]]=totdeltau_oos[-delete,k[j]]+ deltau.lambda_oos
      
      
      
      
      
      x = factor.supp[,1:k[j]]
      reg = lm(X ~ x)
      delta.beta = reg$coefficients[1,]
      
      sigma2 = window/(window - k[j])*valuef.supp[k[j]]
      
      PP.lambda.nodemean_oos[j, i] = mean(delta.lambda_oos^2)
      PP.lambda_oos[j, i] = mean(delta.lambda_oos^2) - (sigma2/window)*(1 - sum(risk_premia.supp[1:k[j]]^2))
      PP.beta[j, i] = mean(delta.beta^2)
      
      
      sigmau2 = windowu/(windowu - k[j])*ts.valuefu[k[j]]
      PP.lambdau.nodemean_oos[j, i] = mean(deltau.lambda_oos^2)
      PP.lambdau_oos[j, i] = mean(deltau.lambda_oos^2) - (sigmau2/window)*(1 - sum(risk_premiau.supp[1:k[j]]^2))
      # 
      PP.lambdau2.nodemean[j, i] = mean(deltau2.lambda^2)
      PP.lambdau2[j, i] = mean(deltau2.lambda^2) - (sigmau2/window)*(1 - sum(risk_premiau.supp[1:k[j]]^2))
      # 
      PP.lambdau3.nodemean[j, i] = mean(deltau3.lambda^2)
      PP.lambdau3[j, i] = mean(deltau3.lambda^2) - (sigmau2/window)*(1 - sum(risk_premia.supp[1:k[j]]^2))
      # 
      uno=matrix(1,window,1)
      aa=uno- ts.factors[,1:k[j],i]%*%as.matrix(risk_premia.supp[1:k[j]])
      bb=as.numeric(window * ( 1 - t(as.matrix(risk_premia.supp[1:k[j]]))%*%as.matrix(risk_premia.supp[1:k[j]])  )  /(window - k[j]))*
        ( diag(window)-  (ts.factors[,1:k[j],i]%*% t(ts.factors[,1:k[j],i]))/window)
      #  G=(1/(TT^2))*( kronecker(aa,aa) - window/(window - k[j])*vec(bb)  )
      sigma4=ts.sigma4[k[j],i]
      
      
      supp3=( aa%*%t(aa)- bb )    #t(G)%*%Ue%*%G
      suppGUG=(sigma4/TT^4)*( (tr(supp3))^2 +  tr( supp3%*%supp3))
      PP.testdelta[j,i]=sqrt(N)*PP.lambda[j,i]/sqrt(suppGUG)
      PP.testdelta2[j,i]=sqrt(N)*(PP.lambda[j,i]-PP.lambdaFF[i])/sqrt(suppGUG)
      
      PP.testdelta_neg[j,i]=PP.lambda[j,i]- 1.96*sqrt(suppGUG/N)
      PP.testdelta_pos[j,i]=PP.lambda[j,i]+ 1.96*sqrt(suppGUG/N)
      
      #  SE of HJ
      
      sdf_temp = SDF_fun( rf[index], k[j], ts.factors[,,i])
      
      sdfu_temp = SDF_fun( rf[index], k[j],ts.factorsu[index,,])
      sdfFF_temp = SDF_fun( rf[index],5,FF[index,])
      
      
      pe_cond_temp_oos=t(sdf_temp)%*%X_oos/window
      pe_uncond_temp_oos=t(sdfu_temp)%*%X_oos/window
      pe_condFF_temp_oos=t(sdfFF_temp)%*%X_oos/window
      
      
      toth_oos[-delete,k[j]]=toth_oos[-delete,k[j]]+ t(pe_cond_temp_oos)
      tothu_oos[-delete,k[j]]=tothu_oos[-delete,k[j]]+ t(pe_uncond_temp_oos)
      tothFF_oos[-delete,k[j]]=tothFF_oos[-delete,k[j]]+ t(pe_condFF_temp_oos)
    
      d=-(t(sdf_temp)%*%sdf_temp)/(window^2*(window-k[j]))
      aa=(1/window)*sdf_temp
      bb=as.numeric(d)*( diag(1,window)-  (ts.factors[,1:k[j],i]%*% t(ts.factors[,1:k[j],i]))/window)
      #  G=(1/(TT^2))*( kronecker(aa,aa) - window/(window - k[j])*vec(bb)  )
      sigma4=ts.sigma4[k[j],i]
      
      supp3=( aa%*%t(aa)- bb )    #t(G)%*%Ue%*%G
      suppGUG=sigma4*( (tr(supp3))^2 +  tr( supp3%*%supp3))
      
      
      
      PP.h.nodemeanFF_oos[j,i]= (pe_condFF_temp_oos)%*%t(pe_condFF_temp_oos)/N 
      
      PP.h.nodemean_oos[j,i]= (pe_cond_temp_oos)%*%t(pe_cond_temp_oos)/N 
      PP.h_oos[j,i]= (pe_cond_temp_oos)%*%t(pe_cond_temp_oos)/N  -sigma2*t(aa)%*%aa   
      PP.testh[j,i]=sqrt(N)*PP.h[j,i]/sqrt(suppGUG)
      PP.testh_neg[j,i]=PP.h[j,i]- 1.96*sqrt(suppGUG/N)
      PP.testh_pos[j,i]=PP.h[j,i]+ 1.96*sqrt(suppGUG/N)
      
      
      
      d=-(t(sdfu_temp)%*%sdfu_temp)/(window^2*(window-k[j]))
      aa=(1/window)*sdfu_temp
      bb=as.numeric(d)*( diag(window)-  (ts.factorsu[index,1:k[j],]%*% t(ts.factorsu[index,1:k[j],]))/window)
      #  G=(1/(TT^2))*( kronecker(aa,aa) - window/(window - k[j])*vec(bb)  )
      sigma4u=ts.sigma4u[j]
   
      supp3=( aa%*%t(aa)- bb )    #t(G)%*%Ue%*%G
      suppGUG=sigma4u*( (tr(supp3))^2 +  tr( supp3%*%supp3))
      
      
      
      PP.hu.nodemean_oos[j,i]= (pe_uncond_temp_oos)%*%t(pe_uncond_temp_oos)/N 
      PP.hu_oos[j,i]= (pe_uncond_temp_oos)%*%t(pe_uncond_temp_oos)/N  -sigmau2*t(aa)%*%aa   
      PP.testhu[j,i]=sqrt(N)*PP.hu[j,i]/sqrt(suppGUG)
      PP.testh_negu[j,i]=PP.hu[j,i]- 1.96*sqrt(suppGUG/N)
      PP.testh_posu[j,i]=PP.hu[j,i]+ 1.96*sqrt(suppGUG/N)
      
      
      
      
      
    }
}
        


# from T/2+1 to T


# here all objects needed for is and oos, both bal and unbal. 
# sdf constructed above and risk premia below.
# sdf
#sdfT is the sdf in sample unbal con at time i+win-1
#sdfu is the sdf in sample bal  uncond at time i+win-1
#sdfTFF is the sdf in sample unbal FF at time i+win-1
# sdfhalfT is 10x1  is bal uncond  pseudo oos for i+w-1
#sdfhalfTcon 1x1  is bal pseudo oos for i+w-1
#sdfhalfTconunbal 1x1   is unbal pseudo oos for i+w-1



# risk premia
#risk_premiau.supp is uncond in sample 10x1
#risk_premiauFF.supp is FF uncond in sample  5x1
#risk_premia.supp  is cond in sample  nw x 10
#riskpremiaFF  nwx5  cond FF

# riskpremia_unbal_pseudo  10 x nw-windows  unbal pseudo cond oos i+win  (occhio: diverso da sdf pseudo!)
# riskpremia_bal_pseudo  10 x nw-windows  bal pseudo cond oos i+win  (occhio: diverso da sdf pseudo!)
#risk_premiau_Thalf 10x1 bal uncond first T/2 obs


i=window
ts.sdfbT=matrix(NA,10,nw)
for (i in window:nw)
{
  
        ts.sdfbT[,i]= ts.sdfb[window,,i]
}


#  START  IN-SAMPLE  


# this is for the bias of the rp delta statistics for unconditional used above



corr_unc_sdf_is=matrix(0,10,1)
corr_unc_rp_is=matrix(0,10,1)

ts.lambdau=(1/T_bal)*t(X_balanced)%*%matrix(ts.factorsu,T_bal,10)
riskpremiau=apply(matrix(ts.factorsu,T_bal,10),2,mean)


j=1
for (j in 1:10)
{
  val_temp = 1/(N_bal*T_bal) * sum((X_balanced -  ts.factorsu[,1:j,1]%*% t(ts.lambdau[,1:j]))^2)
  sigma2 = T_bal/(T_bal - j)*val_temp
  aa=matrix(0,T_bal,1)
  aa=t((1/T_bal)*t(sdfu_vec[j,]))
  corr_unc_sdf_is[j]= sigma2*t(aa)%*%aa  
  
  corr_unc_rp_is[j]= (sigma2/T_bal)*(1-t(riskpremiau[1:j])%*%riskpremiau[1:j]) 
  
  
}




#  here i evalute PE with SE in-sample for both risk premia and SDF for bal and unbal

#  reset  insample rolling
{
  
   
    

  
  
  SS_condFF_is=matrix(NA,1,nw)
  SS_cond_bal_is=matrix(NA,10,nw)
  SS_uncond_bal_is=matrix(NA,10,nw)
  SS_cond_unbal_is=matrix(NA,10,nw)
  
  
  corr_c_sdf_bal_is=matrix(NA,10,1)
  corr_c_rp_bal_is=matrix(NA,10,1)
  
  corr_c_sdf_unbal_is=matrix(NA,10,1)
  corr_c_rp_unbal_is=matrix(NA,10,1)
  
  
  dmSS_cond_bal_is=matrix(NA,10,nw)
  dmSS_uncond_bal_is=matrix(NA,10,nw)
  dmSS_cond_unbal_is=matrix(NA,10,nw)
  
  
  
  
  
  
  
  totSDF_cunis=matrix(NA,NN,10)
  totSDF_cbis=matrix(NA,N_bal,10)
  totSDF_ubis=matrix(NA,N_bal,10)

  totSDF_cunis_tv=matrix(NA,NN,1)
  totSDF_cbis_tv=matrix(NA,N_bal,1)
    
    
 # totSDF_cbis_tv+pe_cond_bal_is[ts.nfactorhat[i],]#lam.supp22%*%apply(totfac22,2,mean)
  
  delta_uncond_bal_is=matrix(NA,N_bal,10)
  totLamPrem_ubis=matrix(NA,N_bal,10)
    
    
  
  #delta con bal and time i-1
  
  # delta_cond_bal_is=matrix(NA,N_bal,10)
  # delta_cond_unbal_is=matrix(NA,N,10)
  # 
  # delta_cond_bal_is_45=matrix(NA,N_bal,10)
  # delta_cond_unbal_is_45=matrix(NA,N,10)
  # 
  # delta_cond_bal_is_tv=matrix(NA,N_bal,1)
  # delta_cond_bal_is_45_tv=matrix(NA,N_bal,1)
  # delta_cond_unbal_is_tv=matrix(NA,N,1)
  # delta_cond_unbal_is_45_tv=matrix(NA,N,1)
  

    totLamPrem_ubis=matrix(0,N_bal,10)
    totLamPrem_cbis=matrix(0,N_bal,10)
    totLamPrem_cunis=matrix(0,NN,10)
    totLamPrem_cbis_tv=matrix(0,N_bal,1)
    totLamPrem_cunis_tv=matrix(0,NN,1)
    

  
  # sum of squared  for delta pricing errors
  
  #SS_cond_oos[1,i]=(pe_cond_oos)%*%t(pe_cond_oos)/N
  #SS_uncond_oos[,i]=(diag((pe_uncond_oos)%*%t(pe_uncond_oos)))/N
  #SS_condFF_oos[1,i]=(pe_condFF_oos)%*%t(pe_condFF_oos)/N
  #SS_uncondFF_oos[1,i]=(pe_uncondFF_oos)%*%t(pe_uncondFF_oos)/N
  SS_delta_cond_bal_is=matrix(NA,10,nw)
  SS_delta_uncond_bal_is=matrix(NA,10,nw)
  SS_delta_cond_unbal_is=matrix(NA,10,nw)
  
  
  SS_delta_cond_bal_is_45=matrix(NA,10,nw)
  SS_delta_cond_unbal_is_45=matrix(NA,10,nw)
  
  
  
  dmSS_delta_cond_bal_is=matrix(NA,10,nw)
  dmSS_delta_uncond_bal_is=matrix(NA,10,nw)
  dmSS_delta_cond_unbal_is=matrix(NA,10,nw)
  
  
  dmSS_delta_cond_bal_is_45=matrix(NA,10,nw)
  dmSS_delta_cond_unbal_is_45=matrix(NA,10,nw)
  
    dmSS_delta_cond_unbal_is_45_opt=matrix(NA,1,nw)
    dmSS_delta_cond_bal_is_45_opt=matrix(NA,1,nw)
    dmSS_cond_unbal_is_opt=matrix(NA,1,nw)
    dmSS_cond_bal_is_opt=matrix(NA,1,nw)
      
  
  totX2is=matrix(0,NN,1)
#    [-delete,]=totX2is[-delete,]+ matrix(X_mean,nrow=N,ncol=1)##
  totXbal2is=matrix(0,N_bal,1)
    
#    totXbal2is+ matrix(X_balanced_mean,nrow=N_bal,ncol=1)##
  


  }


sdfu_vec=matrix(NA,10,TTT)
j=1
for (j in 1:10)
{
  sdfu_vec[j,]=SDF_fun(rf,j,as.matrix(ts.factorsu[,1:j,1],TTT,j))
}



# NOW LOOP INSAMPLE 
i=window


local_sdfb=matrix(NA,window,10)
local_sdf=matrix(NA,window,10)

for (i in window:(nw-window-1))
{
print(i)
index = i:(i + window-1)
rf.supp = rf[index] - 1
X = data[index,] - rf.supp%*%t(rep(1,NN))
X_balanced_local=X_balanced[index,]
N_bal2=ncol(X_balanced_local)# check that same as N_bal


X_temp=X
delete = apply(X_temp, 2, function(x){sum(is.na(x))})
delete = which(delete > 0)
X = X[,-delete]
N = ncol(X)
X_mean=matrix(NA,N,1)
X_mean=apply(X,2,mean)
X_balanced_mean=as.matrix(apply(X_balanced_local, 2, mean)) 


pe_condFF_is=t(sdfFF[,i])%*%X/window
#is pseudo  SDF  over rolling windows of size window

pe_cond_bal_is=matrix(NA,10,N_bal)  #t(sdfhalfTcon_vec[,(i-window+1):i])%*%X_balanced_local/window
pe_cond_unbal_is=matrix(NA,10,N)#sdfhalfTconunbal_vec[i,(i-window+1):i]%*%X/window
pe_uncond_bal_is=matrix(NA,10,N_bal) 

pe_cond_bal_is_tv=matrix(NA,1,N_bal)  #t(sdfhalfTcon_vec[,(i-window+1):i])%*%X_balanced_local/window
pe_cond_unbal_is_tv=matrix(NA,1,N)#sdfhalfTconunbal_vec[i,(i-window+1):i]%*%X/window




j=1
for (j in 1:10)
{
local_sdfb[,j]=SDF_fun(rf[index],j,X_balanced_local)
local_sdf[,j]=SDF_fun(rf[index],j,X)
}

pe_cond_bal_is=t(as.matrix(local_sdfb,window,10))%*%X_balanced_local/window
pe_cond_unbal_is=t(as.matrix(local_sdf,window,10))%*%X/window
pe_uncond_bal_is=sdfu_vec[,i:(i+window-1)]%*%X_balanced_local/window  # check if  i:i+w-1
pe_cond_bal_is_tv=t(as.matrix(local_sdfb[,ts.nfactorhat[i]],window,1))%*%X_balanced_local/window
pe_cond_unbal_is_tv=t(as.matrix(local_sdf[,ts.nfactorhat[i]],window,1))%*%X/window



# sum of squared 

SS_condFF_is[1,i] = (pe_condFF_is)%*%t(pe_condFF_is)/N
SS_cond_bal_is[,i] = (diag(pe_cond_bal_is%*%t(pe_cond_bal_is)))/N_bal2  #t(sdfhalfTcon[(i-window+1):i])%*%X/window
SS_uncond_bal_is[,i] = (diag(pe_uncond_bal_is%*%t(pe_uncond_bal_is)))/N_bal2#=sdfhalfT[,(i-window+1):i]%*%X/window
SS_cond_unbal_is[,i] = (diag(pe_cond_unbal_is%*%t(pe_cond_unbal_is)))/N#=sdfhalfTconunbal[(i-window+1):i]%*%X/window



# this is for con bal cond
corr_c_sdf_bal_is=matrix(NA,10,1)
corr_c_rp_bal_is=matrix(NA,10,1)
j=1
for (j in 1:10)
{
 
  lam.supp=t(X_balanced_local)%*%matrix(ts.factorsb[,j,i],window,j)/N_bal 
  val_temp_bal = 1/(N_bal*window) * sum((X_balanced_local -  matrix(ts.factorsb[,j,i],window,j)%*% t(lam.supp))^2)
  sigma2_bal = window/(window - j)*val_temp_bal
  aa_bal=matrix(0,window,1)
  aa_bal=(1/window)*matrix(ts.sdfb[,j,i],window,1)
  corr_c_sdf_bal_is[j]= sigma2_bal*t(aa_bal)%*%aa_bal
  rp.supp=apply(matrix(ts.factorsb[,j,i],window,j),2,mean)
  corr_c_rp_bal_is[j]= (sigma2_bal/window)*(1-t(rp.supp)%*%rp.supp) 
}



# this is for con unbal

corr_c_sdf_unbal_is=matrix(NA,10,1)
corr_c_rp_unbal_is=matrix(NA,10,1)

j=1
for (j in 1:10)
{
  
  lam.supp=t(X)%*%matrix(ts.factors[,j,i],window,j)/N 
  val_temp_unbal = 1/(N*window) * sum((X -  matrix(ts.factors[,j,i],window,j)%*% t(lam.supp))^2)
  sigma2_unbal = window/(window - j)*val_temp_unbal
  aa_unbal=matrix(0,window,1)
  aa_unbal=(1/window)*sdf_vec[,j,i]
  corr_c_sdf_unbal_is[j]= sigma2_unbal*t(aa_unbal)%*%aa_unbal
  rp.supp=apply(matrix(ts.factors[,j,i],window,j),2,mean)
  corr_c_rp_unbal_is[j]= (sigma2_unbal/window)*(1-t(rp.supp)%*%rp.supp) 
  
  
  
  
  
  
}



dmSS_cond_bal_is[,i]=SS_cond_bal_is[,i]-corr_c_sdf_bal_is#(pe_cond_bal_pseudo_oos%*%t(pe_cond_bal_pseudo_oos))/N_bal2  #t(sdfhalfTcon[(i-window+1):i])%*%X/window
dmSS_uncond_bal_is[,i]=SS_uncond_bal_is[,i]-corr_unc_sdf_is#(diag(pe_uncond_bal_pseudo_oos%*%t(pe_uncond_bal_pseudo_oos)))/N_bal2#=sdfhalfT[,(i-window+1):i]%*%X/window
dmSS_cond_unbal_is[,i]=SS_cond_unbal_is[,i]-corr_c_sdf_unbal_is#(pe_cond_unbal_pseudo_oos%*%t(pe_cond_unbal_pseudo_oos))/N#=sdfhalfTconunbal[(i-window+1):i]%*%X/window

dmSS_cond_unbal_is_opt[,i]=((pe_cond_unbal_is_tv)%*%t(pe_cond_unbal_is_tv))/N-corr_c_sdf_unbal_is[ts.nfactorhat[i]]#(pe_cond_bal_pseudo_oos%*%t(pe_cond_bal_pseudo_oos))/N_bal2  #t(sdfhalfTcon[(i-window+1):i])%*%X/window
dmSS_cond_bal_is_opt[,i]=((pe_cond_bal_is_tv)%*%t(pe_cond_bal_is_tv))/N_bal2-corr_c_sdf_bal_is[ts.nfactorhat[i]]#(pe_cond_unbal_pseudo_oos%*%t(pe_cond_unbal_pseudo_oos))/N#=sdfhalfTconunbal[(i-window+1):i]%*%X/window







# now risk premia delta rolling



totSDF_cunis[-delete,]=totSDF_cunis[-delete,]+t(pe_cond_unbal_is)#lam.supp22%*%apply(totfac22,2,mean)
totSDF_cbis=totSDF_cbis+t(pe_cond_bal_is)#lam.supp22%*%apply(totfac22,2,mean)
totSDF_ubis=totSDF_ubis+t(pe_uncond_bal_is)#lam.supp22%*%apply(totfac22,2,mean)

totSDF_cunis_tv[-delete]=totSDF_cunis_tv[-delete]+pe_cond_unbal_is[ts.nfactorhat[i],]#lam.supp22%*%apply(totfac22,2,mean)
totSDF_cbis_tv=totSDF_cbis_tv+pe_cond_bal_is[ts.nfactorhat[i],]#lam.supp22%*%apply(totfac22,2,mean)







# risk premia full length in  loop




# 10x1
delta_uncond_bal_is=matrix(NA,N_bal,10)
j=1
for (j in 1:10)
{
  Z=matrix((as.matrix(ts.lambdau[,1:j],Nbal,j)%*%riskpremiau[1:j]),N_bal,1)
  regu1is=lm(X_balanced_mean~Z)
  delta_uncond_bal_is[,j]=X_balanced_mean-Z
  totLamPrem_ubis[,j]=totLamPrem_ubis[,j]+ regu1is$fitted.values#lambdau_halfT[,1:j]%*%matrix(risk_premiau_Thalf[1:j])
  
}

delta_cond_bal_is=matrix(NA,N_bal,10)
delta_cond_unbal_is=matrix(NA,N,10)

delta_cond_bal_is_45=matrix(NA,N_bal,10)
delta_cond_unbal_is_45=matrix(NA,N,10)

delta_cond_bal_is_tv=matrix(NA,N_bal,1)
delta_cond_bal_is_45_tv=matrix(NA,N_bal,1)
delta_cond_unbal_is_tv=matrix(NA,N,1)
delta_cond_unbal_is_45_tv=matrix(NA,N,1)




j=1
for (j in 1:10)
{
  lam.supp=t(X_balanced_local)%*%ts.factorsb[,1:j,i]/N_bal 
  Z=lam.supp%*%apply(matrix(ts.factorsb[,1:j,i],window,j),2,mean)
  regc1is=lm(X_balanced_mean~Z)
  #summary(reg)
  delta_cond_bal_is[,j]=regc1is$residuals#X_balanced_mean- lam.supp%*%apply(totfac2,2,mean)
  delta_cond_bal_is_45[,j]=X_balanced_mean-Z#X_balanced_mean- lam.supp%*%apply(totfac2,2,mean)
  totLamPrem_cbis[,j]=totLamPrem_cbis[,j]+regc1is$fitted.values# lam.supp%*%apply(totfac2,2,mean)
  
  
  
  
  
  
 
  # this is delta cond unbal
  lam.supp2=t(X)%*%ts.factors[,1:j,i]/N 
  
  
  Z=lam.supp2%*%apply(matrix(ts.factors[,1:j,i],window,j),2,mean)
  regc2is=lm(X_mean~Z)

  delta_cond_unbal_is[,j]=regc2is$residuals #ßX_mean-lam.supp22%*%apply(totfac22,2,mean)
  delta_cond_unbal_is_45[,j]=X_mean-Z #ßX_mean-lam.supp22%*%apply(totfac22,2,mean)
  
  totLamPrem_cunis[-delete,j]=totLamPrem_cunis[-delete,j]+regc2is$fitted.values#lam.supp22%*%apply(totfac22,2,mean)
  
  
  
  
  
  
  
  
 
  
  
  
  
  
  
  
}





# tv bal

# time varying
lam.supp=t(X_balanced_local)%*%ts.factorsb[,1:ts.nfactorhat[i],i]/N_bal 
Z=lam.supp%*%apply(matrix(ts.factorsb[,1:ts.nfactorhat[i],i],window,ts.nfactorhat[i]),2,mean)
regc1tvis=lm(X_balanced_mean~Z)

delta_cond_bal_is_tv=regc1tvis$residuals#X_balanced_mean- lam.supp%*%apply(totfac2,2,mean)
delta_cond_bal_is_45_tv=X_balanced_mean-Z#X_balanced_mean- lam.supp%*%apply(totfac2,2,mean)
totLamPrem_cbis_tv=totLamPrem_cbis_tv+regc1tvis$fitted.values# lam.supp%*%apply(totfac2,2,mean)


#tv unbal
lam.supp2=t(X)%*%ts.factors[,,i]/N 


if (ts.nfactorhat[i] ==1){ Z=lam.supp2[,1]*mean(ts.factors[,1,i])
} else
{  Z=lam.supp2[,1:ts.nfactorhat[i]]%*%apply(as.matrix(ts.factors[,1:ts.nfactorhat[i],i],window,ts.nfactorhat[i]),2,mean)}
regc3tvis=lm(X_mean~Z)

delta_cond_unbal_is_tv=regc3tvis$residuals#X_mean-lam.supp2%*%apply(ts.factors[,,i],2,mean)
delta_cond_unbal_is_45_tv=X_mean-Z#X_mean-lam.supp2%*%apply(ts.factors[,,i],2,mean)

totLamPrem_cunis_tv[-delete]=totLamPrem_cunis_tv[-delete,1]+ regc3tvis$fitted.values#lam.supp2%*%apply(ts.factors[,,i],2,mean)


# sum of squared  for delta pricing errors


SS_delta_cond_bal_is[,i]=diag(t(delta_cond_bal_is)%*%(delta_cond_bal_is))/N_bal2  #t(sdfhalfTcon[(i-window+1):i])%*%X/window
SS_delta_uncond_bal_is[,i]=(diag(t(delta_uncond_bal_is)%*%(delta_uncond_bal_is)))/N_bal2#=sdfhalfT[,(i-window+1):i]%*%X/window
SS_delta_cond_unbal_is[,i]=diag(t(delta_cond_unbal_is)%*%(delta_cond_unbal_is))/N#=sdfhalfTconunbal[(i-window+1):i]%*%X/window

SS_delta_cond_bal_is_45[,i]=(diag(t(delta_cond_bal_is_45)%*%(delta_cond_bal_is_45)))/N_bal2
SS_delta_cond_unbal_is_45[,i]=diag(t(delta_cond_unbal_is_45)%*%(delta_cond_unbal_is_45))/N











dmSS_delta_cond_bal_is[,i]=SS_delta_cond_bal_is[,i]-corr_c_rp_bal_is#(pe_cond_bal_pseudo_oos%*%t(pe_cond_bal_pseudo_oos))/N_bal2  #t(sdfhalfTcon[(i-window+1):i])%*%X/window
dmSS_delta_uncond_bal_is[,i]=SS_delta_uncond_bal_is[,i]-corr_unc_rp_is#(diag(pe_uncond_bal_pseudo_oos%*%t(pe_uncond_bal_pseudo_oos)))/N_bal2#=sdfhalfT[,(i-window+1):i]%*%X/window
dmSS_delta_cond_unbal_is[,i]=SS_delta_cond_unbal_is[,i]-corr_c_rp_unbal_is#(pe_cond_unbal_pseudo_oos%*%t(pe_cond_unbal_pseudo_oos))/N#=sdfhalfTconunbal[(i-window+1):i]%*%X/window


dmSS_delta_cond_bal_is_45[,i]=SS_delta_cond_bal_is_45[,i]-corr_c_rp_bal_is#(pe_cond_bal_pseudo_oos%*%t(pe_cond_bal_pseudo_oos))/N_bal2  #t(sdfhalfTcon[(i-window+1):i])%*%X/window
dmSS_delta_cond_unbal_is_45[,i]=SS_delta_cond_unbal_is_45[,i]-corr_c_rp_unbal_is#(pe_cond_unbal_pseudo_oos%*%t(pe_cond_unbal_pseudo_oos))/N#=sdfhalfTconunbal[(i-window+1):i]%*%X/window

dmSS_delta_cond_unbal_is_45_opt[,i]=(diag(t(delta_cond_unbal_is_45_tv)%*%(delta_cond_unbal_is_45_tv))/N)-corr_c_rp_unbal_is[ts.nfactorhat[i]]#(pe_cond_bal_pseudo_oos%*%t(pe_cond_bal_pseudo_oos))/N_bal2  #t(sdfhalfTcon[(i-window+1):i])%*%X/window
dmSS_delta_cond_bal_is_45_opt[,i]=(diag(t(delta_cond_bal_is_45_tv)%*%(delta_cond_bal_is_45_tv))/N_bal2)-corr_c_rp_bal_is[ts.nfactorhat[i]]#(pe_cond_unbal_pseudo_oos%*%t(pe_cond_unbal_pseudo_oos))/N#=sdfhalfTconunbal[(i-window+1):i]%*%X/window



totX2is[-delete,]=totX2is[-delete,]+ matrix(X_mean,nrow=N,ncol=1)##
totXbal2is=totXbal2is+ matrix(X_balanced_mean,nrow=N_bal,ncol=1)##

}
# correlations is
t(cor(totLamPrem_cbis,totXbal2is))
(cor(totLamPrem_cbis_tv,totXbal2is))

t(cor(totLamPrem_ubis,totXbal2is))

t(cor(totLamPrem_cunis,totX2is))
(cor(totLamPrem_cunis_tv,totX2is))


# END OF IN-SAMPLE
# START OF IN-SAMPLE

# reset quantities:
{
SS_cond_oos=matrix(NA,1,nw)
SS_uncond_oos=matrix(NA,1,nw)
SS_condFF_oos=matrix(NA,1,nw)
SS_cond_bal_oos=matrix(NA,10,nw)
SS_uncond_bal_oos2=matrix(NA,10,nw)
SS_uncond_bal_oos=matrix(NA,10,nw)
SS_cond_unbal_oos=matrix(NA,10,nw)
SS_cond_unbal_oos2=matrix(NA,10,nw)


dmSS_cond_bal_oos=matrix(NA,10,nw)
dmSS_uncond_bal_oos2=matrix(NA,10,nw)
dmSS_uncond_bal_oos=matrix(NA,10,nw)
dmSS_cond_unbal_oos=matrix(NA,10,nw)
dmSS_cond_unbal_oos2=matrix(NA,10,nw)

dmSS_cond_unbal_oos2_opt=matrix(NA,1,nw)
dmSS_cond_unbal_oos_opt=matrix(NA,1,nw)
dmSS_cond_bal_oos_opt=matrix(NA,1,nw)




mean_cond_oos=matrix(NA,1,nw)
mean_uncond_oos=matrix(NA,1,nw)
mean_condFF_oos=matrix(NA,1,nw)

mean_cond_bal_oos=matrix(NA,10,nw)
mean_uncond_bal_oos2=matrix(NA,10,nw)
mean_uncond_bal_oos=matrix(NA,10,nw)
mean_cond_unbal_oos=matrix(NA,10,nw)
mean_cond_unbal2_oos=matrix(NA,10,nw)


mean_acond_bal_oos=matrix(NA,10,nw)
mean_auncond_bal_oos2=matrix(NA,10,nw)
mean_auncond_bal_oos=matrix(NA,10,nw)
mean_acond_unbal_oos=matrix(NA,10,nw)
mean_acond_unbal_oos2=matrix(NA,10,nw)



# delta_uncond_bal_pseudo_oos=matrix(NA,10,nw)
# delta_uncond_bal_pseudo_oos2=matrix(NA,10,nw)
# delta_cond_bal_pseudo_oos=matrix(NA,1,nw)
# delta_cond_unbal_pseudo_oos=matrix(NA,1,nw)
# delta_cond_unbal_pseudo_oos2=matrix(NA,1,nw)





SS_delta_cond_bal_oos=matrix(NA,10,nw)
SS_delta_uncond_bal_oos2=matrix(NA,10,nw)
SS_delta_uncond_bal_oos=matrix(NA,10,nw)
SS_delta_cond_unbal_oos=matrix(NA,10,nw)
SS_delta_cond_unbal_oos2=matrix(NA,10,nw)

dmSS_delta_cond_bal_oos=matrix(NA,10,nw)
dmSS_delta_uncond_bal_oos2=matrix(NA,10,nw)
dmSS_delta_uncond_bal_oos=matrix(NA,10,nw)
dmSS_delta_cond_unbal_oos=matrix(NA,10,nw)
dmSS_delta_cond_unbal_oos2=matrix(NA,10,nw)


SS_delta_cond_bal_oos_45=matrix(NA,10,nw)
SS_delta_cond_unbal_oos_45=matrix(NA,10,nw)
SS_delta_cond_unbal_oos2_45=matrix(NA,10,nw)

dmSS_delta_cond_bal_oos_45=matrix(NA,10,nw)
dmSS_delta_cond_unbal_oos_45=matrix(NA,10,nw)
dmSS_delta_cond_unbal_oos2_45=matrix(NA,10,nw)


dmSS_delta_cond_unbal_oos2_45_opt=matrix(NA,1,nw)
dmSS_delta_cond_unbal_oos_45_opt=matrix(NA,1,nw)
dmSS_delta_cond_bal_oos_45_opt=matrix(NA,1,nw)



mean_delta_cond_bal_oos=matrix(NA,10,nw)
mean_delta_uncond_bal_oos2=matrix(NA,10,nw)
mean_delta_uncond_bal_oos=matrix(NA,10,nw)
mean_delta_cond_unbal_oos=matrix(NA,10,nw)
mean_delta_cond_unbal_oos2=matrix(NA,10,nw)


mean_delta_acond_bal_oos=matrix(NA,10,nw)
mean_delta_auncond_bal_oos2=matrix(NA,10,nw)
mean_delta_auncond_bal_oos=matrix(NA,10,nw)
mean_delta_acond_unbal_oos=matrix(NA,10,nw)
mean_delta_acond_unbal_oos2=matrix(NA,10,nw)




totLamPrem_ubpoos=matrix(0,nrow=N_bal,ncol=10)
totLamPrem_ubpoos2=matrix(0,nrow=N_bal,ncol=10)
totLamPrem_cbpoos=matrix(0,nrow=N_bal,ncol=10)
totLamPrem_cunpoos=matrix(0,nrow=NN,ncol=10)
totLamPrem_cunpoos2=matrix(0,nrow=NN,ncol=10)

totLamPrem_cunpoos_tv=matrix(0,nrow=NN,ncol=1)
totLamPrem_cunpoos2_tv=matrix(0,nrow=NN,ncol=1)
totLamPrem_cbpoos_tv=matrix(0,nrow=N_bal,ncol=1)



totSDF_cunpoos=matrix(0,nrow=NN,ncol=10)
totSDF_cunpoos2=matrix(0,nrow=NN,ncol=10)
totSDF_cbpoos=matrix(0,nrow=N_bal,ncol=10)
totSDF_ubpoos=matrix(0,nrow=N_bal,ncol=10)
totSDF_ubpoos2=matrix(0,nrow=N_bal,ncol=10)
totSDF_cunpoos_tv=matrix(0,nrow=NN,ncol=1)
totSDF_cunpoos2_tv=matrix(0,nrow=NN,ncol=1)
totSDF_cbpoos_tv=matrix(0,nrow=N_bal,ncol=1)

  




totX2=matrix(0,nrow=NN,ncol=1)
totXbal2=matrix(0,nrow=N_bal,ncol=1)



}

#  PE  OOS


i=(Thalf-window+2)
for (i in (Thalf-window+2):(nw-window))# i take away -window from nw because of rolling oos

{
  print(i)
  
  
  index = i:(i + window-1)
  index_oos = (i+window):(i + 2*window-1)
  
  rf.supp = rf[index] - 1
  rf.oos = rf[index_oos] - 1

  X = data[index,]
  X_oos=data[index_oos,]

  X_balanced_local=X_balanced[index,]
  N_bal2=ncol(X_balanced_local)# check that same as N_bal
  
  if (!is.na(ts.deletestocks[[1]][1]))
  {
    delete = ts.deletestocks[[i]]
    if (class(delete) != "NULL") X = X[,-delete]
  }

  if (!is.na(ts.deletestocks[[1]][1]))
  {
    delete_oos = ts.deletestocks[[(i+window)]]
    if (class(delete_oos) != "NULL") X_oos = X_oos[,-delete_oos]
  }


 
  
   N=ncol(X)
  X = as.matrix(X) - rf.supp %*% t(rep(1, N))
  
  
  N_oos = ncol(X_oos)
  X_oos = as.matrix(X_oos) - rf.oos %*% t(rep(1, N_oos))
  
  
  X_balanced_local = as.matrix(X_balanced_local) - rf.supp %*% t(rep(1, N_bal2))
  N_bal_loc = ncol(X_balanced_local)
  
  
  
  sdfuu=as.matrix(sdfu[index,45])
  sdfuuFF=as.matrix(sdfuFF[index,45])
  
  
  lambdau=lambdau.supp[-delete,]
  lambdauFF=lambdau.suppFF[-delete,]
  
  
 
  X_mean = as.matrix(apply(X, 2, mean)) #from i to i+win-1
  X_mean_oos =as.matrix( apply(X_oos, 2, mean))# from i+win to i+2win-1
  X_balanced_mean=as.matrix(apply(X_balanced_local, 2, mean)) 
  

  
  
  
  # in sample unbal cond rolling 
  factor.supp = ts.factors[,,i]
  lambda.supp = t(1/window *(t(factor.supp) %*% X))
  risk_premia.supp = apply(factor.supp, 2, mean)
  valuef.supp = ts.valuef[, i]
  
  k = c(ts.nfactorhat[i], k0)

  
 
  pe_cond_oos=t(sdf[,i])%*%X_oos/window
  pe_uncond_oos=t(sdfuu)%*%X_oos/window
  pe_condFF_oos=t(sdfFF[,i])%*%X_oos/window
#oos pseudo  SDF  over rolling windows of size window
  pe_cond_bal_pseudo_oos=matrix(NA,10,N_bal)  #t(sdfhalfTcon_vec[,(i-window+1):i])%*%X_balanced_local/window
  pe_cond_unbal_pseudo_oos=matrix(NA,10,N)#sdfhalfTconunbal_vec[i,(i-window+1):i]%*%X/window
  pe_cond_unbal_pseudo_oos2=matrix(NA,10,N)#t(sdf_vec[,,(i-1)])%*%X/window
  
  
  
  pe_cond_bal_pseudo_oos=(sdfhalfTcon_vec[,(i-window+1):i])%*%X_balanced_local/window
  pe_cond_unbal_pseudo_oos=sdfhalfTconunbal_vec[,(i-window+1):i]%*%X/window
  pe_cond_unbal_pseudo_oos2=t(sdf_vec[,,(i-1)])%*%X/window
  
  pe_cond_bal_oos_tv=(sdfhalfTcon_vec[ts.nfactorhat[i],(i-window+1):i])%*%X_balanced_local/window
  pe_cond_unbal_oos_tv=sdfhalfTconunbal_vec[ts.nfactorhat[i],(i-window+1):i]%*%X/window
  pe_cond_unbal_oos2_tv=t(sdf_vec[,ts.nfactorhat[i],(i-1)])%*%X/window
    

# this is 10x1
   pe_uncond_bal_pseudo_oos2=as.matrix(apply(sdfhalfT[,2:(Thalf-window+1)],1,mean),10,1)%*%rep(1,1,window)%*%X_balanced_local/window
  
  #  this is 10x1
  pe_uncond_bal_pseudo_oos=sdfhalfT[,(i-window+1):i]%*%X_balanced_local/window
   
    
    

    

  
  
  
  # sum of squared 
  
  SS_cond_oos[1,i] = (pe_cond_oos)%*%t(pe_cond_oos)/N
  SS_uncond_oos[,i] = (diag((pe_uncond_oos)%*%t(pe_uncond_oos)))/N
  SS_condFF_oos[1,i] = (pe_condFF_oos)%*%t(pe_condFF_oos)/N
  SS_cond_bal_oos[,i] = (diag(pe_cond_bal_pseudo_oos%*%t(pe_cond_bal_pseudo_oos)))/N_bal2  #t(sdfhalfTcon[(i-window+1):i])%*%X/window
  SS_uncond_bal_oos2[,i] = (diag(pe_uncond_bal_pseudo_oos2%*%t(pe_uncond_bal_pseudo_oos2)))/N_bal2#=as.matrix(sdfhalfT[,(Thalf-window+1)],10,1)%*%rep(1,1,window)%*%X/window
  SS_uncond_bal_oos[,i] = (diag(pe_uncond_bal_pseudo_oos%*%t(pe_uncond_bal_pseudo_oos)))/N_bal2#=sdfhalfT[,(i-window+1):i]%*%X/window
  SS_cond_unbal_oos[,i] = (diag(pe_cond_unbal_pseudo_oos%*%t(pe_cond_unbal_pseudo_oos)))/N#=sdfhalfTconunbal[(i-window+1):i]%*%X/window
  SS_cond_unbal_oos2[,i] = (diag(pe_cond_unbal_pseudo_oos2%*%t(pe_cond_unbal_pseudo_oos2)))/N#=sdf[(i-window):(i-1)]%*%X/window
  
  
  
  # this is for con bal cond
  corr_c_sdf_bal=matrix(NA,10,1)
  corr_c_rp_bal=matrix(NA,10,1)
  j=1
  for (j in 1:10)
  {
     totfac2bal=matrix(NA,window,j)
    totfac2bal=rbind(as.matrix(Fbal2_vec_vec[2:window,1:j,i],window-1,j),t(fhalfcon2_vec_vec[1:j,i]))
    lam.supp=t(X_balanced_local)%*%matrix(totfac2bal,window,j)/N_bal 
    val_temp_bal = 1/(N_bal*window) * sum((X_balanced_local -  totfac2bal%*% t(lam.supp))^2)
    sigma2_bal = window/(window - j)*val_temp_bal
    aa_bal=matrix(0,window,1)
    aa_bal=(1/window)*sdfhalfTcon_vec[j,(i-window+1):i]
    corr_c_sdf_bal[j]= sigma2_bal*t(aa_bal)%*%aa_bal
    rp.supp=apply(totfac2bal,2,mean)
    corr_c_rp_bal[j]= (sigma2_bal/window)*(1-t(rp.supp)%*%rp.supp) 
  }
        
    # this is for con unbal
    
  corr_c_sdf_unbal=matrix(NA,10,1)
  corr_c_rp_unbal=matrix(NA,10,1)
  corr_c_sdf_unbal2=matrix(NA,10,1)
  corr_c_rp_unbal2=matrix(NA,10,1)
  
    j=1
  for (j in 1:10)
  {
    
    
    
    totfac2unbal=rbind(as.matrix(Fbal2unbal_vec_vec[2:window,1:j,i],window-1,j),t(fhalfcon2unbal_vec_vec[1:j,i]))
    lam.supp=t(X)%*%matrix(totfac2unbal,window,j)/N 
    val_temp_unbal = 1/(N*window) * sum((X -  totfac2unbal%*% t(lam.supp))^2)
    sigma2_unbal = window/(window - j)*val_temp_unbal
    aa_unbal=matrix(0,window,1)
    aa_unbal=(1/window)*sdfhalfTconunbal_vec[j,(i-window+1):i]
    corr_c_sdf_unbal[j]= sigma2_unbal*t(aa_unbal)%*%aa_unbal
    rp.supp=apply(totfac2unbal,2,mean)
    corr_c_rp_unbal[j]= (sigma2_unbal/window)*(1-t(rp.supp)%*%rp.supp) 
    
    
    
        
  # this for con unbal 2
  
    lam.supp=t(X)%*%matrix(Fbal2unbal_vec_vec[,1:j,i],window,j)/N 
    val_temp_unbal2 = 1/(N*window) * sum((X -  Fbal2unbal_vec_vec[,1:j,i]%*% t(lam.supp))^2)
    sigma2_unbal2 = window/(window - j)*val_temp_unbal2
    aa_unbal2=matrix(0,window,1)
    aa_unbal2=(1/window)*sdf_vec[,j,(i-1)] 
    corr_c_sdf_unbal2[j]= sigma2_unbal2*t(aa_unbal2)%*%aa_unbal2
    rp.supp=apply(matrix(Fbal2unbal_vec_vec[,1:j,i],window,j),2,mean)
    corr_c_rp_unbal2[j]= (sigma2_unbal2/window)*(1-t(rp.supp)%*%rp.supp) 
    
    
  }
    
    

  dmSS_cond_bal_oos[,i]=SS_cond_bal_oos[,i]-corr_c_sdf_bal#(pe_cond_bal_pseudo_oos%*%t(pe_cond_bal_pseudo_oos))/N_bal2  #t(sdfhalfTcon[(i-window+1):i])%*%X/window
  dmSS_uncond_bal_oos2[,i]= SS_uncond_bal_oos2[,i]-corr_unc_sdf
  dmSS_uncond_bal_oos[,i]=SS_uncond_bal_oos[,i]-corr_unc_sdf#(diag(pe_uncond_bal_pseudo_oos%*%t(pe_uncond_bal_pseudo_oos)))/N_bal2#=sdfhalfT[,(i-window+1):i]%*%X/window
  dmSS_cond_unbal_oos[,i]=SS_cond_unbal_oos[,i]-corr_c_sdf_unbal#(pe_cond_unbal_pseudo_oos%*%t(pe_cond_unbal_pseudo_oos))/N#=sdfhalfTconunbal[(i-window+1):i]%*%X/window
  dmSS_cond_unbal_oos2[,i]=SS_cond_unbal_oos2[,i]-corr_c_sdf_unbal2#(pe_cond_unbal_pseudo_oos2%*%t(pe_cond_unbal_pseudo_oos2))/N#=sdf[(i-window):(i-1)]%*%X/window
  
  dmSS_cond_unbal_oos2_opt[,i]=((pe_cond_unbal_oos2_tv)%*%t(pe_cond_unbal_oos2_tv))/N-corr_c_sdf_unbal[ts.nfactorhat[i]]#(pe_cond_bal_pseudo_oos%*%t(pe_cond_bal_pseudo_oos))/N_bal2  #t(sdfhalfTcon[(i-window+1):i])%*%X/window
  dmSS_cond_unbal_oos_opt[,i]=((pe_cond_unbal_oos_tv)%*%t(pe_cond_unbal_oos_tv))/N-corr_c_sdf_unbal[ts.nfactorhat[i]]#(pe_cond_bal_pseudo_oos%*%t(pe_cond_bal_pseudo_oos))/N_bal2  #t(sdfhalfTcon[(i-window+1):i])%*%X/window
  dmSS_cond_bal_oos_opt[,i]=((pe_cond_bal_oos_tv)%*%t(pe_cond_bal_oos_tv))/N_bal2-corr_c_sdf_bal[ts.nfactorhat[i]]#(pe_cond_unbal_pseudo_oos%*%t(pe_cond_unbal_pseudo_oos))/N#=sdfhalfTconunbal[(i-window+1):i]%*%X/window
  
  
  
  
  # mean
  
  mean_cond_oos[1,i]=mean(pe_cond_oos)
  mean_uncond_oos[,i]=mean(pe_uncond_oos)
  mean_condFF_oos[1,i]=mean(pe_condFF_oos)
  
  mean_cond_bal_oos[,i]=apply(pe_cond_bal_pseudo_oos,1,mean)#%*%t(pe_cond_bal_pseudo_oos))/N_bal2  #t(sdfhalfTcon[(i-window+1):i])%*%X/window
  mean_uncond_bal_oos2[,i]=apply(pe_uncond_bal_pseudo_oos2,1,mean)#%*%t(pe_uncond_bal_pseudo_oos_Thalf))/N_bal2#=as.matrix(sdfhalfT[,(Thalf-window+1)],10,1)%*%rep(1,1,window)%*%X/window
  mean_uncond_bal_oos[,i]=apply(pe_uncond_bal_pseudo_oos,1,mean)#%*%t(pe_uncond_bal_pseudo_oos))/N_bal2#=sdfhalfT[,(i-window+1):i]%*%X/window
  mean_cond_unbal_oos[,i]=apply(pe_cond_unbal_pseudo_oos,1,mean)#%*%t(pe_cond_unbal_pseudo_oos))/N#=sdfhalfTconunbal[(i-window+1):i]%*%X/window
  mean_cond_unbal2_oos[,i]=apply(pe_cond_unbal_pseudo_oos2,1,mean)#%*%t(pe_cond_unbal_pseudo2_oos))/N#=sdf[(i-window):(i-1)]%*%X/window
  
  
  

  
  mean_acond_oos[1,i]=mean(abs(pe_cond_oos))
  mean_auncond_oos[,i]=mean(abs(pe_uncond_oos))
  mean_acondFF_oos[1,i]=mean(abs(pe_condFF_oos))
  
  mean_acond_bal_oos[,i]=apply(abs(pe_cond_bal_pseudo_oos),1,mean)#%*%t(pe_cond_bal_pseudo_oos))/N_bal2  #t(sdfhalfTcon[(i-window+1):i])%*%X/window
  mean_auncond_bal_oos2[,i]=apply(abs(pe_uncond_bal_pseudo_oos2),1,mean)#%*%t(pe_uncond_bal_pseudo_oos_Thalf))/N_bal2#=as.matrix(sdfhalfT[,(Thalf-window+1)],10,1)%*%rep(1,1,window)%*%X/window
  mean_auncond_bal_oos[,i]=apply(abs(pe_uncond_bal_pseudo_oos),1,mean)#%*%t(pe_uncond_bal_pseudo_oos))/N_bal2#=sdfhalfT[,(i-window+1):i]%*%X/window
  mean_acond_unbal_oos[,i]=apply(abs(pe_cond_unbal_pseudo_oos),1,mean)#%*%t(pe_cond_unbal_pseudo_oos))/N#=sdfhalfTconunbal[(i-window+1):i]%*%X/window
  mean_acond_unbal_oos2[,i]=apply(abs(pe_cond_unbal_pseudo_oos2),1,mean)#%*%t(pe_cond_unbal_pseudo2_oos))/N#=sdf[(i-window):(i-1)]%*%X/window
  

  
  # now risk premia delta rolling
  
  pe_cond_unbal_pseudo_oos=sdfhalfTconunbal_vec[,(i-window+1):i]%*%X/window
  pe_cond_unbal_pseudo_oos2=t(sdf_vec[,,(i-1)])%*%X/window
  
  
  totSDF_cunpoos[-delete,]=totSDF_cunpoos[-delete,]+t(pe_cond_unbal_pseudo_oos)#lam.supp22%*%apply(totfac22,2,mean)
  totSDF_cunpoos2[-delete,]=totSDF_cunpoos2[-delete,]+t(pe_cond_unbal_pseudo_oos2)#lam.supp22%*%apply(totfac22,2,mean)
  totSDF_cbpoos=totSDF_cbpoos+t(pe_cond_bal_pseudo_oos)#lam.supp22%*%apply(totfac22,2,mean)
  totSDF_ubpoos=totSDF_ubpoos+t(pe_uncond_bal_pseudo_oos)#lam.supp22%*%apply(totfac22,2,mean)
  totSDF_ubpoos2=totSDF_ubpoos2+t(pe_uncond_bal_pseudo_oos2)#lam.supp22%*%apply(totfac22,2,mean)

  totSDF_cunpoos_tv[-delete]=totSDF_cunpoos_tv[-delete]+pe_cond_unbal_pseudo_oos[ts.nfactorhat[i],]#lam.supp22%*%apply(totfac22,2,mean)
  totSDF_cunpoos2_tv[-delete]=totSDF_cunpoos2_tv[-delete]+pe_cond_unbal_pseudo_oos2[ts.nfactorhat[i],]#lam.supp22%*%apply(totfac22,2,mean)
  totSDF_cbpoos_tv=totSDF_cbpoos_tv+pe_cond_bal_pseudo_oos[ts.nfactorhat[i],]#lam.supp22%*%apply(totfac22,2,mean)
  
  
  
  
  

  
  # risk premia T/2
  
  
  
  
  # 10x1
  delta_uncond_bal_pseudo_oos=matrix(NA,N_bal,10)
  j=1
  for (j in 1:10)
  {
    Z=matrix((lambdau_halfT[,1:j]%*%matrix(risk_premiau_Thalf[1:j])),N_bal,1)
    regu1=lm(X_balanced_mean~Z)
   delta_uncond_bal_pseudo_oos[,j]=X_balanced_mean-matrix((lambdau_halfT[,1:j]%*%matrix(risk_premiau_Thalf[1:j])),N_bal,1)
   totLamPrem_ubpoos[,j]=totLamPrem_ubpoos[,j]+ regu1$fitted.values#lambdau_halfT[,1:j]%*%matrix(risk_premiau_Thalf[1:j])
   
   }
 
  # 10x1
  delta_uncond_bal_pseudo_oos2=matrix(NA,N_bal,10)
  j=1  
  risk_premiau_Thalf2=apply(fhalfv[,(i-window+1):i],1,mean) # factorhalf is pseudo bal uncond T/2 x 10
  for (j in 1:10)
  {
    Z=matrix((lambdau_halfT[,1:j]%*%matrix(risk_premiau_Thalf2[1:j])),N_bal,1)
    regu2=lm(X_balanced_mean~Z)
    totLamPrem_ubpoos2[,j]=totLamPrem_ubpoos2[,j]+ regu2$fitted.values#lambdau_halfT[,1:j]%*%matrix(risk_premiau_Thalf2[1:j])}
    
    delta_uncond_bal_pseudo_oos2[,j]=X_balanced_mean-(lambdau_halfT[,1:j]%*%matrix(risk_premiau_Thalf2[1:j]))}
 
  
  #delta con bal and time i-1
  
  delta_cond_bal_pseudo_oos=matrix(NA,N_bal,10)
  delta_cond_unbal_pseudo_oos=matrix(NA,N,10)
  delta_cond_unbal_pseudo_oos2=matrix(NA,N,10)
  
  delta_cond_bal_pseudo_oos_45=matrix(NA,N_bal,10)
  delta_cond_unbal_pseudo_oos_45=matrix(NA,N,10)
  delta_cond_unbal_pseudo_oos2_45=matrix(NA,N,10)
  
  delta_cond_bal_pseudo_oos_tv=matrix(NA,N_bal,1)
  delta_cond_bal_pseudo_oos_45_tv=matrix(NA,N_bal,1)
  delta_cond_unbal_pseudo_oos_tv=matrix(NA,N,1)
  delta_cond_unbal_pseudo_oos2_tv=matrix(NA,N,1)
  delta_cond_unbal_pseudo_oos_45_tv=matrix(NA,N,1)
  delta_cond_unbal_pseudo_oos2_45_tv=matrix(NA,N,1)
  
  
  
  
  j=1
  for (j in 1:10)
  {
    totfac2=rbind(as.matrix(Fbal2_vec2_vec[2:window,,i],window-1,10),t(fhalfcon2_vec_vec[,i]))
    
    totfac2=matrix(totfac2,window,j)
  
  
  
  
  lam.supp=t(X_balanced_local)%*%totfac2/N_bal 
  Z=lam.supp%*%apply(matrix(totfac2,window,j),2,mean)
  regc1=lm(X_balanced_mean~Z)

      delta_cond_bal_pseudo_oos[,j]=regc1$residuals#X_balanced_mean- lam.supp%*%apply(totfac2,2,mean)
      delta_cond_bal_pseudo_oos_45[,j]=X_balanced_mean-Z#X_balanced_mean- lam.supp%*%apply(totfac2,2,mean)
      totLamPrem_cbpoos[,j]=totLamPrem_cbpoos[,j]+regc1$fitted.values# lam.supp%*%apply(totfac2,2,mean)
      
   
     
      
         
      

     # this is delta cond unbal
  totfac22=rbind(as.matrix(Fbal2unbal_vec_vec[2:window,,i],window-1,10),t(fhalfcon2unbal_vec_vec[,i]))
  totfac22=matrix(totfac22,window,j)
  lam.supp22=t(X)%*%totfac22/N
  lam.supp2=t(X)%*%ts.factors[,,i]/N 
  
  
  Z=lam.supp22%*%apply(totfac22,2,mean)
  regc2=lm(X_mean~Z)

  delta_cond_unbal_pseudo_oos[,j]=regc2$residuals #ßX_mean-lam.supp22%*%apply(totfac22,2,mean)
  delta_cond_unbal_pseudo_oos_45[,j]=X_mean-Z #ßX_mean-lam.supp22%*%apply(totfac22,2,mean)
  
  totLamPrem_cunpoos[-delete,j]=totLamPrem_cunpoos[-delete,j]+regc2$fitted.values#lam.supp22%*%apply(totfac22,2,mean)
  
  
   
  
  
  
  
  

  if (j ==1){ Z=lam.supp2[,1]*mean(ts.factors[,1,i])
  } else
{  Z=lam.supp2[,1:j]%*%apply(as.matrix(ts.factors[,1:j,i],window,j),2,mean)}
  regc3=lm(X_mean~Z)
  #summary(reg)
  delta_cond_unbal_pseudo_oos2[,j]=regc3$residuals#X_mean-lam.supp2%*%apply(ts.factors[,,i],2,mean)
  delta_cond_unbal_pseudo_oos2_45[,j]=X_mean-Z#X_mean-lam.supp2%*%apply(ts.factors[,,i],2,mean)
 
  totLamPrem_cunpoos2[-delete,j]=totLamPrem_cunpoos2[-delete,j]+ regc3$fitted.values#lam.supp2%*%apply(ts.factors[,,i],2,mean)
  
  
  
  


  
  }
  
  
  
  
  
  # tv bal
  
  # time varying
  totfac2=rbind(as.matrix(Fbal2_vec2_vec[2:window,,i],window-1,10),t(fhalfcon2_vec_vec[,i]))
  totfac2=matrix(totfac2,window,ts.nfactorhat[i])
  lam.supp=t(X_balanced_local)%*%totfac2/N_bal 
  Z=lam.supp%*%apply(matrix(totfac2,window,ts.nfactorhat[i]),2,mean)
  regc1tv=lm(X_balanced_mean~Z)
  #summary(reg)
  delta_cond_bal_pseudo_oos_tv[,1]=regc1tv$residuals#X_balanced_mean- lam.supp%*%apply(totfac2,2,mean)
  delta_cond_bal_pseudo_oos_45_tv[,1]=X_balanced_mean-Z#X_balanced_mean- lam.supp%*%apply(totfac2,2,mean)
  totLamPrem_cbpoos_tv=totLamPrem_cbpoos_tv+regc1tv$fitted.values# lam.supp%*%apply(totfac2,2,mean)
  
  
  #tv unbal
  #tv
  totfac22=rbind(as.matrix(Fbal2unbal_vec_vec[2:window,,i],window-1,10),t(fhalfcon2unbal_vec_vec[,i]))
  totfac22=matrix(totfac22,window,ts.nfactorhat[i])
  lam.supp22=t(X)%*%totfac22/N
  lam.supp2=t(X)%*%ts.factors[,,i]/N 
  
  
  Z=lam.supp22%*%apply(totfac22,2,mean)
  regc2tv=lm(X_mean~Z)
 
  delta_cond_unbal_pseudo_oos_tv=regc2tv$residuals #ßX_mean-lam.supp22%*%apply(totfac22,2,mean)
  delta_cond_unbal_pseudo_oos_45_tv=X_mean-Z #ßX_mean-lam.supp22%*%apply(totfac22,2,mean)
  
  totLamPrem_cunpoos_tv[-delete]=totLamPrem_cunpoos_tv[-delete]+regc2tv$fitted.values#lam.supp22%*%apply(totfac22,2,mean)
  
  
  
  #tv  unbal2
  if (ts.nfactorhat[i] ==1){ Z=lam.supp2[,1]*mean(ts.factors[,1,i])
  } else
  {  Z=lam.supp2[,1:ts.nfactorhat[i]]%*%apply(as.matrix(ts.factors[,1:ts.nfactorhat[i],i],window,ts.nfactorhat[i]),2,mean)}
  regc3tv=lm(X_mean~Z)
  
  delta_cond_unbal_pseudo_oos2[,ts.nfactorhat[i]]=regc3tv$residuals#X_mean-lam.supp2%*%apply(ts.factors[,,i],2,mean)
  delta_cond_unbal_pseudo_oos2_45_tv=X_mean-Z#X_mean-lam.supp2%*%apply(ts.factors[,,i],2,mean)
  
  totLamPrem_cunpoos2_tv[-delete]=totLamPrem_cunpoos2_tv[-delete,1]+ regc3tv$fitted.values#lam.supp2%*%apply(ts.factors[,,i],2,mean)
  
  
  SS_delta_cond_bal_oos[,i]=diag(t(delta_cond_bal_pseudo_oos)%*%(delta_cond_bal_pseudo_oos))/N_bal2  #t(sdfhalfTcon[(i-window+1):i])%*%X/window
  
  SS_delta_uncond_bal_oos2[,i]=(diag(t(delta_uncond_bal_pseudo_oos2)%*%(delta_uncond_bal_pseudo_oos2)))/N_bal2#=as.matrix(sdfhalfT[,(Thalf-window+1)],10,1)%*%rep(1,1,window)%*%X/window
  SS_delta_uncond_bal_oos[,i]=(diag(t(delta_uncond_bal_pseudo_oos)%*%(delta_uncond_bal_pseudo_oos)))/N_bal2#=sdfhalfT[,(i-window+1):i]%*%X/window
  
  SS_delta_cond_unbal_oos[,i]=diag(t(delta_cond_unbal_pseudo_oos)%*%(delta_cond_unbal_pseudo_oos))/N#=sdfhalfTconunbal[(i-window+1):i]%*%X/window
  SS_delta_cond_unbal_oos2[,i]=diag(t(delta_cond_unbal_pseudo_oos2)%*%(delta_cond_unbal_pseudo_oos2))/N#=sdf[(i-window):(i-1)]%*%X/window
  
  
  SS_delta_cond_bal_oos_45[,i]=diag(t(delta_cond_bal_pseudo_oos_45)%*%(delta_cond_bal_pseudo_oos_45))/N_bal2  #t(sdfhalfTcon[(i-window+1):i])%*%X/window
  SS_delta_cond_unbal_oos_45[,i]=diag(t(delta_cond_unbal_pseudo_oos_45)%*%(delta_cond_unbal_pseudo_oos_45))/N#=sdfhalfTconunbal[(i-window+1):i]%*%X/window
  SS_delta_cond_unbal_oos2_45[,i]=diag(t(delta_cond_unbal_pseudo_oos2_45)%*%(delta_cond_unbal_pseudo_oos2_45))/N#=sdf[(i-window):(i-1)]%*%X/window
  
  
  
    
  
  
  
  
  
  
    
  dmSS_delta_cond_bal_oos[,i]=SS_delta_cond_bal_oos[,i]-corr_c_rp_bal#(pe_cond_bal_pseudo_oos%*%t(pe_cond_bal_pseudo_oos))/N_bal2  #t(sdfhalfTcon[(i-window+1):i])%*%X/window
  dmSS_delta_uncond_bal_oos2[,i]= SS_delta_uncond_bal_oos2[,i]-corr_unc_rp
  dmSS_delta_uncond_bal_oos[,i]=SS_delta_uncond_bal_oos[,i]-corr_unc_rp#(diag(pe_uncond_bal_pseudo_oos%*%t(pe_uncond_bal_pseudo_oos)))/N_bal2#=sdfhalfT[,(i-window+1):i]%*%X/window
  dmSS_delta_cond_unbal_oos[,i]=SS_delta_cond_unbal_oos[,i]-corr_c_rp_unbal#(pe_cond_unbal_pseudo_oos%*%t(pe_cond_unbal_pseudo_oos))/N#=sdfhalfTconunbal[(i-window+1):i]%*%X/window
  dmSS_delta_cond_unbal_oos2[,i]=SS_delta_cond_unbal_oos2[,i]-corr_c_rp_unbal2#(pe_cond_unbal_pseudo_oos2%*%t(pe_cond_unbal_pseudo_oos2))/N#=sdf[(i-window):(i-1)]%*%X/window

  
  dmSS_delta_cond_bal_oos_45[,i]=SS_delta_cond_bal_oos_45[,i]-corr_c_rp_bal#(pe_cond_bal_pseudo_oos%*%t(pe_cond_bal_pseudo_oos))/N_bal2  #t(sdfhalfTcon[(i-window+1):i])%*%X/window
  dmSS_delta_cond_unbal_oos_45[,i]=SS_delta_cond_unbal_oos_45[,i]-corr_c_rp_unbal#(pe_cond_unbal_pseudo_oos%*%t(pe_cond_unbal_pseudo_oos))/N#=sdfhalfTconunbal[(i-window+1):i]%*%X/window
  dmSS_delta_cond_unbal_oos2_45[,i]=SS_delta_cond_unbal_oos2_45[,i]-corr_c_rp_unbal2#(pe_cond_unbal_pseudo_oos2%*%t(pe_cond_unbal_pseudo_oos2))/N#=sdf[(i-window):(i-1)]%*%X/window
  
  
  
  dmSS_delta_cond_unbal_oos2_45_opt[,i]=t(delta_cond_unbal_pseudo_oos2_45_tv)%*%(delta_cond_unbal_pseudo_oos2_45_tv)/N -  corr_c_rp_unbal2[ts.nfactorhat[i]]
  dmSS_delta_cond_unbal_oos_45_opt[,i]=t(delta_cond_unbal_pseudo_oos_45_tv)%*%(delta_cond_unbal_pseudo_oos_45_tv)/N -  corr_c_rp_unbal[ts.nfactorhat[i]]
  dmSS_delta_cond_bal_oos_45_opt[,i]=t(delta_cond_bal_pseudo_oos_45_tv)%*%(delta_cond_bal_pseudo_oos_45_tv)/N -  corr_c_rp_bal[ts.nfactorhat[i]]

    

    
 
  # mean
  

  mean_delta_cond_bal_oos[,i]=apply(delta_cond_bal_pseudo_oos,2,mean)#%*%t(pe_cond_bal_pseudo_oos))/N_bal2  #t(sdfhalfTcon[(i-window+1):i])%*%X/window
  
  mean_delta_uncond_bal_oos2[,i]=apply(delta_uncond_bal_pseudo_oos2,2,mean)#%*%t(pe_uncond_bal_pseudo_oos_Thalf))/N_bal2#=as.matrix(sdfhalfT[,(Thalf-window+1)],10,1)%*%rep(1,1,window)%*%X/window
  mean_delta_uncond_bal_oos[,i]=apply(delta_uncond_bal_pseudo_oos,2,mean)#%*%t(pe_uncond_bal_pseudo_oos))/N_bal2#=sdfhalfT[,(i-window+1):i]%*%X/window
  
  mean_delta_cond_unbal_oos[,i]=apply(delta_cond_unbal_pseudo_oos,2,mean)#%*%t(pe_cond_unbal_pseudo_oos))/N#=sdfhalfTconunbal[(i-window+1):i]%*%X/window
  mean_delta_cond_unbal_oos2[,i]=apply(delta_cond_unbal_pseudo_oos2,2,mean)#%*%t(pe_cond_unbal_pseudo2_oos))/N#=sdf[(i-window):(i-1)]%*%X/window
  
  
  mean_delta_acond_bal_oos[,i]=apply(abs(delta_cond_bal_pseudo_oos),2,mean)#%*%t(pe_cond_bal_pseudo_oos))/N_bal2  #t(sdfhalfTcon[(i-window+1):i])%*%X/window
  
  mean_delta_auncond_bal_oos2[,i]=apply(abs(delta_uncond_bal_pseudo_oos2),2,mean)#%*%t(pe_uncond_bal_pseudo_oos_Thalf))/N_bal2#=as.matrix(sdfhalfT[,(Thalf-window+1)],10,1)%*%rep(1,1,window)%*%X/window
  mean_delta_auncond_bal_oos[,i]=apply(abs(delta_uncond_bal_pseudo_oos),2,mean)#%*%t(pe_uncond_bal_pseudo_oos))/N_bal2#=sdfhalfT[,(i-window+1):i]%*%X/window
  
  mean_delta_acond_unbal_oos[,i]=apply(abs(delta_cond_unbal_pseudo_oos),2,mean)#%*%t(pe_cond_unbal_pseudo_oos))/N#=sdfhalfTconunbal[(i-window+1):i]%*%X/window
  mean_delta_acond_unbal_oos2[,i]=apply(abs(delta_cond_unbal_pseudo_oos2),2,mean)#%*%t(pe_cond_unbal_pseudo2_oos))/N#=sdf[(i-window):(i-1)]%*%X/window
  
  
  
 
  totX2[-delete,]=totX2[-delete,]+ matrix(X_mean,nrow=N,ncol=1)##
  totXbal2=totXbal2+ matrix(X_balanced_mean,nrow=N_bal,ncol=1)##
  
 



}




# END OF Pricing Perfomance




#  formal and informal plots   PE SDF IS and OOS

{
# this is in-sample constant PCA
totLamPrem_ubis=matrix(0,N_bal,10)
delta_uncond_bal_is=matrix(NA,N_bal,10)
j=1  
risk_premiau=apply(ts.factorsu,2,mean) # factorhalf is pseudo bal uncond T/2 x 10
lambdauu=t(X_balanced)%*%matrix(ts.factorsu,nrow=TTT,ncol=10)/TTT
for (j in 1:10)
{
  
  Z=matrix((lambdauu[,1:j]%*%matrix(risk_premiau[1:j])),N_bal,1)
  regu22=lm(X_balanced_mean~Z)
  totLamPrem_ubis[,j]=totLamPrem_ubis[,j]+ regu22$fitted.values#lambdau_halfT[,1:j]%*%matrix(risk_premiau_Thalf2[1:j])}
  
  delta_uncond_bal_is[,j]=X_balanced_mean-(lambdauu[,1:j]%*%matrix(risk_premiau[1:j]))}



totX2=totX2/(nw-Thalf-1)
totXbal2=totXbal2/(nw-Thalf-1)
totLamPrem_ubpoos=totLamPrem_ubpoos/(nw-Thalf-1)
totLamPrem_ubpoos2=totLamPrem_ubpoos2/(nw-Thalf-1)
totLamPrem_cbpoos=totLamPrem_cbpoos/(nw-Thalf-1)
totLamPrem_cunpoos2=totLamPrem_cunpoos2/(nw-Thalf-1)
totLamPrem_cunpoos=totLamPrem_cunpoos/(nw-Thalf-1)
totLamPrem_cbpoos_tv=totLamPrem_cbpoos_tv/(nw-Thalf-1)
totLamPrem_cunpoos2_tv=totLamPrem_cunpoos2_tv/(nw-Thalf-1)
totLamPrem_cunpoos_tv=totLamPrem_cunpoos_tv/(nw-Thalf-1)


totSDF_cunpoos=totSDF_cunpoos/(nw-Thalf-1)
totSDF_cunpoos2=totSDF_cunpoos2/(nw-Thalf-1)
totSDF_cbpoos=totSDF_cbpoos/(nw-Thalf-1)
totSDF_ubpoos=totSDF_ubpoos/(nw-Thalf-1)
totSDF_ubpoos2=totSDF_ubpoos2/(nw-Thalf-1)

totSDF_cunpoos_tv=totSDF_cunpoos_tv/(nw-Thalf-1)
totSDF_cunpoos2_tv=totSDF_cunpoos2_tv/(nw-Thalf-1)
totSDF_cbpoos_tv=totSDF_cbpoos_tv/(nw-Thalf-1)

plot(totSDF_ubpoos[,1])
points(totSDF_ubpoos2[,1],col=2)
points(totSDF_cbpoos[,1],col=3)

#  PPPQQQ

c1 <- rgb(173,216,230, max = 255, alpha = 150, names = "lt.blue")
c2 <- rgb(255,192,203, max = 255, alpha = 150, names = "lt.pink")
c3 <- rgb(144,238,144, max = 255, alpha = 150, names = "lt.green")
c4 <- rgb(255,0,0, max = 255, alpha = 150, names = "lt.red")
c11 <- rgb(173,216,230, max = 255, alpha = 255, names = "lt.blue")
c33 <- rgb(144,238,144, max = 255, alpha = 255, names = "lt.green")
c44 <- rgb(255,0,0, max = 255, alpha = 255, names = "lt.red")
c22 <- rgb(255,192,203, max = 255, alpha = 255, names = "lt.pink")

hSDF3 <- hist(totSDF_ubpoos[,7], breaks = 30 , freq= FALSE)
hSDF2 <- hist(totSDF_cbpoos[,7], breaks = 30 ,  freq=FALSE)
hSDF <- hist(totSDF_cunpoos[,7], breaks = 30 ,  freq=FALSE)
#hSDF <- hist(totSDF_cunpoos[,1], breaks = 30 , plot = FALSE , freq=FALSE)


pdf('T120_SDF_K7_9Jan23.pdf')
plot(hSDF,  main="
            Pricing Performance: Pricing Errors SDF
\n Out-of-Sample: local PCA  vs. constant PCA  (T=120,r=7) \n" ,#xlim=c(-3,3),ylim=c(0,25),
     xlab="Out-of-Sample Pricing Errors SDF",col = 1 , freq = FALSE) # Plot 1st histogram using a transparent color
plot(hSDF3,  col = c4, add = TRUE , freq=FALSE) # Add 2nd histogram using different color
plot(hSDF2,  col = c3, add = TRUE , freq=FALSE) # Add 2nd histogram using different color

#abline(coef = c(0,1),col=1,lwd=5) 
legend(0.01,100,c("local PCA unb", "local PCA bal", "constant PCA"), lwd=c(5,5), col=c(1,c3,c4), y.intersp=0.75)
dev.off()





totXbal2=as.matrix(totXbal2)
totX2=as.matrix(totX2)


plot(totLamPrem_cbpoos[,1],totXbal2)
points(totLamPrem_ubpoos[,1],totXbal2,col=2)
points(totLamPrem_ubpoos2[,1],totXbal2,col=3)
abline(coef = c(0,1))  


t(cor(totLamPrem_cbpoos,totXbal2))# questa
t(cor(totLamPrem_cunpoos,totX2))# questa
t(cor(totLamPrem_cunpoos2,totX2))


t(cor(totLamPrem_cbpoos_tv,totXbal2))# questa
t(cor(totLamPrem_cunpoos_tv,totX2))# questa
t(cor(totLamPrem_cunpoos2_tv,totX2))


t(cor(totLamPrem_ubpoos2,totXbal2))# questa
t(cor(totLamPrem_ubpoos,totXbal2))


c1 <- rgb(173,216,230, max = 255, alpha = 100, names = "lt.blue")
c2 <- rgb(255,192,203, max = 255, alpha = 30, names = "lt.pink")
c3 <- rgb(144,238,144, max = 255, alpha = 100, names = "lt.green")
c4 <- rgb(255,0,0, max = 255, alpha = 100, names = "lt.red")
c11 <- rgb(173,216,230, max = 255, alpha = 255, names = "lt.blue")
c33 <- rgb(144,238,144, max = 255, alpha = 255, names = "lt.green")
c44 <- rgb(255,0,0, max = 255, alpha = 255, names = "lt.red")
c22 <- rgb(255,192,203, max = 255, alpha = 255, names = "lt.pink")

#k=1 only


#  change T24 or T120 depending on T!
pdf('T120_45_K10_9Jan23.pdf')
plot(totLamPrem_cunpoos2[,10],totX2,  main="
            Pricing Performance: Expected Return
\n   Out-of-Sample - local PCA unbalanced (T=120, r=10) \n", #,xlim=c(-3,3),ylim=c(0,25),
     xlab="Model-Implied Expected Return",ylab="Average Return",   col = c1) # Plot 1st histogram using a transparent color
abline(coef = c(0,1),col=1,lwd=5) 
legend(0,0.04,c("local PCA unb", "45 degrees line"), lwd=c(5,5), col=c(c11,1), y.intersp=0.75)
dev.off()






pdf('T120_45bal_K10_9Jan23.pdf')
plot(totLamPrem_cbpoos[,10],totXbal2,  main="
            Pricing Performance: Expected Return
\n   Out-of-Sample: local PCA  bal vs. constant PCA  (T=120,r=10) \n", #,xlim=c(-3,3),ylim=c(0,25),
     xlab="Model-Implied Expected Return",ylab="Average Return",   col = c33) # Plot 1st histogram using a transparent color
points(totLamPrem_ubpoos[,10],totXbal2,  col = c44) # Add 2nd histogram using different color
abline(coef = c(0,1),col=1,lwd=5) 
legend(0.003,0.02,c("local PCA bal","constant PCA", "45 degrees line"), lwd=c(5,5), col=c(c33,c44,1), y.intersp=0.75)
dev.off()






plot(totLamPrem_cunpoos2[,1],totX2)
#points(totLamPrem_ubpoos2[,1],totXbal2,col=2)
abline(coef = c(0,1))  

plot(totLamPrem_cbpoos[,1],totXbal2)
points(totLamPrem_ubpoos2[,1],totXbal2,col=2)
abline(coef = c(0,1))  


#    sss <- data.frame(cbind(totX,totLamPrem,totLamPremFF,totLamPremFFu,totLamPremu))
sss <- data.frame(cbind(totXbal2,totLamPrem_cbpoos,totLamPrem_ubpoos[,1],totLamPrem_ubpoos2[,1]))



#http://r-statistics.co/Top50-Ggplot2-Visualizations-MasterList-R-Code.html
#http://r-statistics.co/ggplot2-Tutorial-With-R.html
###   pdf('45degree24.pdf')  
# #   ggplot(sss) + geom_point(aes(x=totLamPrem[,1], y=totX, color=3)) +geom_smooth(aes(x=totLamPrem[,1], y=totX, color=4))+geom_abline(mapping = NULL,data = NULL, slope=1, intercept=0, na.rm = FALSE,show.legend = NA)
#    gg<-ggplot(sss) + geom_point(aes(x=totLamPrem[,1], y=totX, colour=3))+ 
gg<-ggplot(sss) + geom_point(aes(x=totLamPrem_cbpoos, y=totXbal2))+ 
  
  #      # geom_point(aes(x=totLamPremu[,1], y=totX, color=5)) +
  #      # geom_point(aes(x=totLamPremFF[,1], y=totX, colour=6)) +
  geom_abline(mapping = NULL,data = NULL, slope=1, intercept=0, na.rm = FALSE, show.legend = F, col="blue" , lwd=3 , lty=2)+
  geom_smooth(method = "lm", aes(x=totLamPrem_cbpoos, y=totXbal2, colour="cond PCA"))+
  geom_smooth(method = "lm", aes(x=totLamPrem_ubpoos[,1], y=totXbal2, colour="uncon PCA"))+
  geom_smooth(method = "lm", aes(x=totLamPrem_ubpoos2[,1], y=totXbal2, colour="uncon PCA2"))+
  scale_colour_manual(name="models",values=c("blue","green", "red"))
gg+ xlab("estimated expected excess return") +
  labs(title="Sample Returns vs. Model-Implied Expected Returns  (T=24)")+
  ylab("sample excess return")
dev.off()    

  cbind(totXbal2,totLamPrem_cbpoos,totLamPrem_ubpoos[,1],totLamPrem_ubpoos2[,1])
  plot(totXbal2,totLamPrem_ubpoos[,1])#+geom_abline(intercept = 0, slope = 1)
  abline(coef = c(0,1))
  
  
  plot(totLamPrem_cbpoos,totLamPrem_ubpoos2[,1])
  abline(coef = c(0,1))
 
  plot(totLamPrem_cbpoos,totXbal2)
  abline(coef = c(0,1))
 
   
   
  

sss <- data.frame(cbind(totX2,totLamPrem_cunpoos,totLamPrem_cunpoos2))



#http://r-statistics.co/Top50-Ggplot2-Visualizations-MasterList-R-Code.html
#http://r-statistics.co/ggplot2-Tutorial-With-R.html
###   pdf('45degree24.pdf')  
# #   ggplot(sss) + geom_point(aes(x=totLamPrem[,1], y=totX, color=3)) +geom_smooth(aes(x=totLamPrem[,1], y=totX, color=4))+geom_abline(mapping = NULL,data = NULL, slope=1, intercept=0, na.rm = FALSE,show.legend = NA)
#    gg<-ggplot(sss) + geom_point(aes(x=totLamPrem[,1], y=totX, colour=3))+ 
gg<-ggplot(sss) + geom_point(aes(x=totLamPrem_cunpoos, y=totX2))+ 
  
  #      # geom_point(aes(x=totLamPremu[,1], y=totX, color=5)) +
  #      # geom_point(aes(x=totLamPremFF[,1], y=totX, colour=6)) +
  geom_abline(mapping = NULL,data = NULL, slope=1, intercept=0, na.rm = FALSE, show.legend = F, col="blue" , lwd=3 , lty=2)+
  geom_smooth(method = "lm", aes(x=totLamPrem_cunpoos, y=totX2, colour="cond PCA"))+
  geom_smooth(method = "lm", aes(x=totLamPrem_cunpoos2, y=totX2, colour="con PCA2"))+
 # geom_smooth(method = "lm", aes(x=totLamPrem_ubpoos2[,1], y=totXbal2, colour="uncon PCA2"))+
  scale_colour_manual(name="models",values=c("blue","green"))
gg+ xlab("estimated expected excess return") +
  labs(title="Sample Returns vs. Model-Implied Expected Returns  (T=24)")+
  ylab("sample excess return")
dev.off()    


plot(totX2,totLamPrem_cunpoos)
abline(coef = c(0,1))

plot(totX2,totLamPrem_cunpoos2)
abline(coef = c(0,1))




# plot  h (SDF)
T1=(Thalf-window+2)
T2=nw-window
plot(SS_cond_oos[T1:T2])
lines(SS_uncond_oos[T1:T2],col=2)
lines(SS_condFF_oos[T1:T2],col=3)


plot(SS_cond_bal_oos[T1:T2])
lines(SS_uncond_bal_oos[1,T1:T2],col=2)
lines(SS_uncond_bal_oos2[1,T1:T2],col=4)
lines(SS_cond_unbal_oos[T1:T2],col=3)
lines(SS_cond_unbal_oos2[T1:T2],col=5)
lines(SS_uncond_bal_oos2[3,T1:T2],col=3)
lines(SS_uncond_bal_oos2[5,T1:T2],col=5)
lines(SS_uncond_bal_oos2[7,T1:T2],col=7)
lines(SS_uncond_bal_oos2[10,T1:T2],col=10)






plot(SS_cond_bal_oos[T1:T2],ylim=c(-0.005,0.005))
lines(dmSS_cond_bal_oos[T1:T2],col=2)
abline(h=0,col=4)

mean(SS_cond_bal_oos[T1:T2])
apply(SS_uncond_bal_oos[,T1:T2],1,mean)
apply(SS_uncond_bal_oos2[,T1:T2],1,mean)
mean(SS_cond_unbal_oos[T1:T2])
mean(SS_cond_unbal_oos2[T1:T2])




plot(dmSS_cond_bal_oos[T1:T2])
lines(dmSS_uncond_bal_oos[1,T1:T2],col=2)
lines(dmSS_cond_unbal_oos[T1:T2],col=3)
lines(dmSS_cond_unbal_oos2[T1:T2],col=4)

abline(h=dm_SS_uncond[1],col=3)
abline(h=0,col=4)
lines(dmSS_uncond_bal_oos2[1,T1:T2],col=4)
lines(dmSS_uncond_bal_oos2[3,T1:T2],col=3)
lines(dmSS_uncond_bal_oos2[5,T1:T2],col=5)
lines(dmSS_uncond_bal_oos2[7,T1:T2],col=7)
lines(dmSS_uncond_bal_oos2[10,T1:T2],col=10)


lines(dmSS_cond_unbal_oos[T1:T2],col=3)
lines(dmSS_cond_unbal_oos2[T1:T2],col=5)

mean(dmSS_cond_bal_oos[T1:T2])
apply(dmSS_uncond_bal_oos[,T1:T2],1,mean)
apply(dmSS_uncond_bal_oos2[,T1:T2],1,mean)
mean(dmSS_cond_unbal_oos[T1:T2])
mean(dmSS_cond_unbal_oos2[T1:T2])


dm_SS_uncond
dm_SS_uncond2


#plot delta rp
T1=(Thalf-window+2)
T2=nw-window

plot(SS_delta_cond_bal_oos[T1:T2],ylim=c(0,0.001))
lines(SS_delta_uncond_bal_oos[10,T1:T2],col=2)
lines(SS_delta_uncond_bal_oos2[1,T1:T2],col=4)
lines(SS_delta_cond_unbal_oos[T1:T2],col=3)
lines(SS_delta_cond_unbal_oos2[T1:T2],col=5)
mean(SS_delta_cond_bal_oos[T1:T2])
mean(SS_delta_cond_unbal_oos[T1:T2])
mean(SS_delta_cond_unbal_oos2[T1:T2])
apply(SS_delta_uncond_bal_oos[,T1:T2],1,mean)
mean(SS_delta_uncond_bal_oos2[1,T1:T2])


plot(dmSS_delta_cond_bal_oos[T1:T2],ylim=c(-0.0015,0.0015))
lines(dmSS_delta_cond_unbal_oos[T1:T2],col=2)
lines(dmSS_delta_cond_unbal_oos2[T1:T2],col=3)
lines(dmSS_delta_uncond_bal_oos[10,T1:T2],col=5)
lines(dmSS_delta_uncond_bal_oos2[10,T1:T2],col=6)
abline(h=dm_SS_delta_uncond[1],col=3)
abline(h=0,col=4)


plot(SS_delta_uncond_bal_oos[1,T1:T2],ylim=c(-0.0005,0.0005))
lines(dmSS_delta_uncond_bal_oos[1,T1:T2],col=2)


plot(SS_delta_uncond_bal_oos2[1,T1:T2],ylim=c(-0.0005,0.0005))
lines(dmSS_delta_uncond_bal_oos2[1,T1:T2],col=2)


plot(SS_delta_cond_unbal_oos[T1:T2],ylim=c(-0.0005,0.0005))
lines(dmSS_delta_cond_unbal_oos[T1:T2],col=2)


plot(SS_delta_cond_unbal_oos2[T1:T2],ylim=c(-0.0005,0.0005))
lines(dmSS_delta_cond_unbal_oos2[T1:T2],col=2)




plot(dmSS_delta_uncond_bal_oos[10,T1:T2],ylim=c(min(dmSS_delta_cond_bal_oos[T1:T2]),max(dmSS_delta_uncond_bal_oos[1,T1:T2])))
lines(dmSS_delta_cond_bal_oos[T1:T2],col=2)
lines(dmSS_delta_cond_unbal_oos[T1:T2],col=3)
lines(dmSS_delta_cond_unbal_oos2[T1:T2],col=5)
abline(h=dm_SS_delta_uncond[1],col=3)
abline(h=0,col=4)

mean(dmSS_delta_cond_bal_oos[T1:T2])
mean(dmSS_delta_cond_unbal_oos[T1:T2])
mean(dmSS_delta_cond_unbal_oos2[T1:T2])
apply(dmSS_delta_uncond_bal_oos[,T1:T2],1,mean)
apply(dmSS_delta_uncond_bal_oos2[,T1:T2],1,mean)


mean(dmSS_delta_uncond_bal_oos2[1,T1:T2])/mean(dmSS_delta_cond_bal_oos[T1:T2])
mean(dmSS_delta_uncond_bal_oos2[1,T1:T2])/mean(dmSS_delta_cond_unbal_oos[T1:T2])
mean(dmSS_delta_uncond_bal_oos2[1,T1:T2])/mean(dmSS_delta_cond_unbal_oos2[T1:T2])

  
  


lines(dmSS_uncond_bal_oos2[1,T1:T2],col=4)
lines(dmSS_cond_unbal_oos[T1:T2],col=3)
lines(dmSS_cond_unbal_oos2[T1:T2],col=5)

dm_SS_delta_uncond
dm_SS_delta_uncond2




# mean

plot(mean_cond_oos[T1:T2])
lines(mean_uncond_oos[T1:T2],col=2)
lines(mean_condFF_oos[T1:T2],col=3)

plot(mean_cond_bal_oos[T1:T2])
lines(mean_uncond_bal_oos[1,T1:T2],col=2)
lines(mean_uncond_bal_oos2[1,T1:T2],col=4)
lines(mean_cond_unbal_oos[T1:T2],col=3)
lines(mean_cond_unbal2_oos[T1:T2],col=5)



plot(mean_acond_oos[T1:T2])
lines(mean_auncond_oos[1,T1:T2],col=2)
lines(mean_acondFF_oos[T1:T2],col=3)

plot(mean_acond_bal_oos[T1:T2])
lines(mean_auncond_bal_oos[1,T1:T2],col=2)
lines(mean_auncond_bal_oos2[1,T1:T2],col=4)
lines(mean_acond_unbal_oos[T1:T2],col=3)
lines(mean_acond_unbal_oos2[T1:T2],col=5)






# now risk premia delta rolling


plot(SS_cond_oos[1,2:409])
points(SS_uncond_oos[1,2:409],col=2)
points(SS_condFF_oos[1,2:409],col=3)
plot(mean_cond_oos[1,2:409])
points(mean_uncond_oos[1,2:409],col=2)
points(mean_condFF_oos[1,2:409],col=3)
plot(mean_acond_oos[1,2:409])
points(mean_auncond_oos[1,2:409],col=2)
points(mean_acondFF_oos[1,2:409],col=3)

plot(PP.lambda_oos[1,2:409])
points(PP.lambdau_oos[1,2:409],col=2)
points(PP.lambdaFF_oos[2:409],col=3)

plot(PP.lambda[1,2:409],ylim=c(-0.0005,0.001926123))
points(PP.lambdau[1,2:409],col=2)
points(PP.lambdaFF[2:409],col=3)
plot(PP.lambda_oos[2:409],col=1)
points(PP.lambdau_oos[2:409],col=5)


plot(PP.h_oos[1,2:409])
points(PP.hu_oos[1,2:409],col=2)
points(PP.h.nodemeanFF_oos[2:409],col=3)
points(PP.hu.nodemean_oos[2:409],col=5)

plot(PP.h[1,2:409],ylim=c(-0.0005,0.0005))
points(PP.hu[1,2:409],col=2)

plot(PP.h.nodemean[1,2:409])#,ylim=c(-0.0005,0.0005))
points(PP.hu.nodemean[1,2:409],col=2)






# end of OOS



# plots is v oos



# #
# ylim = c(min(SR.portt_T24[(1+1):(nw24-140),1]), max(SR.portt_T24[(1+1):(nw24-140),1]))
# #
# #ylim = c(min(meandelta_T120[1,(1+43):(nw120-60)]), 10*max(meandelta_T120[1,(1+43):(nw120-60)]))
# #ylim = c(0,1)
# #name = paste(folder_6, "PP_lambda", ".png", sep = "")
# #png(name, width = 800, height = 500)
# #
# #plot(x = ts.enddate[aa24+42:(nw24-81)], y = meanh_T24[1,1+42:(nw24-81)],col="black",lwd=2, type = "l", main = "Portfolio Performance:  \nConditional v Unconditional v FF5    (T=24)", xlab = "Date", ylab = "Canonical Correlation", ylim = ylim)
# #
# plot(x = ts.enddate[(aa24+1):(nw24-140)], y = SR.portt_T24[(1+1):(nw24-140),1],col="black",lwd=2, type = "l", main = "Portfolio Performance (S&P 500): \nSharpe Ratio - Tangency Portolios \nConditional vs. Unconditional vs. FF5    (T=24)", xlab = "Date", ylab = "Sharpe Ratios (annualized)", ylim = ylim)
# abline(v = rec[which(rec > 0)], col = "grey80")
# #lines(x = ts.enddate[(aa24+1):(nw24-140)], y = SR.porttu_T24[(1+1):(nw24-140),1], lty=1,lwd=1,col=3)
# lines(x = ts.enddate[(aa24+1):(nw24-140)], y =  SR.porttFF_T24[(1+1):(nw24-140),1], lty=1,lwd=1,col=4)
# lines(x = ts.enddate[(aa24+1):(nw24-140)], y =  CI.SR.pos3_T24[(1+1):(nw24-140),1], lty=1,lwd=1,col=2)
# lines(x = ts.enddate[(aa24+1):(nw24-140)], y = CI.SR.neg3_T24[(1+1):(nw24-140),1], lty=1,lwd=1,col=2)
# #lines(x = ts.enddate[(aa24+1):(nw24-140)], y = SR.portt_T24[(1+1):(nw24-140),7], lty=1,lwd=1,col=6)
# # lines(x = ts.enddate[aa120:(nw24-60)], y = conFF120tot[1:(nw120-60),1],lty=1 , lwd=1,col=4)
# # lines(x = ts.enddate[aa120:nw24], y = sdf120do[1:nw120,1],lty=4 , lwd=1,col="grey50")
# #lines(x = ts.enddate[aa24:nw24], y = Rsu24[1:nw24,1], lty=1,lwd=1,col=2)
# lines(x = ts.enddate[aa24+1:(nw24-140)], y = 0.4315598*matrix(1,(nw24-140),1),col="chartreuse4",type ="l")
# legend(7000,-0.5,c("cond","CI+","CI-", "uncond",  "cond FF5" ,  "fin crises"), lwd=c(1.5,1.5), col=c(1,2,2,"chartreuse4",4,"grey80"), y.intersp=0.5)
# dev.off()




#testh_pos_T120= read.csv("T120Results_Alberto/ testh_pos_T120.csv", stringsAsFactors = F)
testh_pos_T24due= read.csv(" testh_pos_T24due.csv", stringsAsFactors = F)
testh_neg_T24due= read.csv(" testh_neg_T24due.csv", stringsAsFactors = F)
meanh_T24dueoos= read.csv("meanh_T24dueoos.csv", stringsAsFactors = F)
meanh_T24due= read.csv("meanh_T24due.csv", stringsAsFactors = F)
meanhu_T24dueoos= read.csv("meandhu_T24dueoos.csv", stringsAsFactors = F)
meanhu_T24due= read.csv("meandhu_T24due.csv", stringsAsFactors = F)



#[(1+24):(nw24+24)], y = meandelta_T24[1,1:(nw24)]

###pdf('h120oos.pdf')
ylim = c(min(meanh_T24due[1,1:(nw24-window)]),0.65*max(meanh_T24dueoos[1,1:(nw24-window)]))
#ylim = c(0,1)
#name = paste(folder_6, "PP_lambda", ".png", sep = "")
#png(name, width = 800, height = 500)
plot(x = ts.enddate[(1+24*2):(nw24+24)], y = meanh_T24due[1,1:(nw24-window)],col="black",lwd=2, type = "l", main = "Pricing Performance of SDF: Conditional    \n In-Sample vs.  Out-of-Sample   (T=24)", xlab = "Date", ylab = "Test Pricing Errors", ylim = ylim)
abline(v = rec[which(rec > 0)], col = "grey80")
lines(x = ts.enddate[(1+24*2):(nw24+24)], y =  testh_pos_T24due[1,1:(nw24-window)], lty=1,lwd=1,col=2)
lines(x = ts.enddate[(1+24*2):(nw24+24)], y =  testh_neg_T24due[1,1:(nw24-window)], lty=1,lwd=1,col=2)
lines(x = ts.enddate[(1+24*2):(nw24+24)], y =meanh_T24dueoos[1,1:(nw24-window)], lty=1,lwd=1,col=5)
legend(17000,0.0001,c("in-sample","CI+","CI-", "out-of-sample",   "econ-fin crises"), lwd=c(1.5,1.5), col=c(1,2,2,5,"grey80"), y.intersp=0.5)
dev.off()


apply(meanh_T24due[,1:(nw24-window)],1,mean)
apply(meanh_T24dueoos[,1:(nw24-window)],1,mean)
apply(meanhu_T24due[,1:(nw24-window)],1,mean)
apply(meanhu_T24dueoos[,1:(nw24-window)],1,mean)



testh_pos_T24due= read.csv(" testh_pos_T24due.csv", stringsAsFactors = F)
testh_neg_T24due= read.csv("T24dueResults_Alberto/ testh_neg_T24due.csv", stringsAsFactors = F)
meanh_T24dueoos= read.csv("T24dueResults_Alberto/meanh_T24dueoos.csv", stringsAsFactors = F)
meanh_T24due= read.csv("T24dueResults_Alberto/meanh_T24due.csv", stringsAsFactors = F)
meanhu_T24dueoos= read.csv("T24dueResults_Alberto/meandhu_T24dueoos.csv", stringsAsFactors = F)
meanhu_T24due= read.csv("T24dueResults_Alberto/meandhu_T24due.csv", stringsAsFactors = F)


apply(meanh_T24due[,43:409],1,mean)
apply(meanh_T24dueoos[,43:409],1,mean)
apply(meanhu_T24due[,43:409],1,mean)
apply(meanhu_T24dueoos[,43:409],1,mean)



                                       
#pdf('h24oos.pdf')
  ylim = c(min(meanh_T24due[1,1:(nw24-window)]),max(meanh_T24dueoos[1,1:(nw24-window)]))
  #ylim = c(0,1)
  #name = paste(folder_6, "PP_lambda", ".png", sep = "")
  #png(name, width = 800, height = 500)
  plot(x = ts.enddate[(1+24*2):(nw24+24)], y = meanh_T24due[1,1:(nw24-window)],col="black",lwd=2, type = "l", main = "Pricing Performance of SDF: Conditional    \n In-Sample vs.  Out-of-Sample   (T=24)", xlab = "Date", ylab = "Test Pricing Errors", ylim = ylim)
  abline(v = rec[which(rec > 0)], col = "grey80")
  lines(x = ts.enddate[(1+24*2):(nw24+24)], y =  testh_pos_T24due[1,1:(nw24-window)], lty=1,lwd=1,col=2)
  lines(x = ts.enddate[(1+24*2):(nw24+24)], y =  testh_neg_T24due[1,1:(nw24-window)], lty=1,lwd=1,col=2)
  lines(x = ts.enddate[(1+24*2):(nw24+24)], y =meanh_T24dueoos[1,1:(nw24-window)], lty=1,lwd=1,col=5)
  legend(17000,0.00015,c("in-sample","CI+","CI-", "out-of-sample",   "fin crises"), lwd=c(1.5,1.5), col=c(1,2,2,5,"grey80"), y.intersp=0.5)
dev.off()







testdelta_pos_T24due= read.csv("T24dueResults_Alberto/ PP.testdelta_pos_T24due.csv", stringsAsFactors = F)
testdelta_neg_T24due= read.csv("T24dueResults_Alberto/ PP.testdelta_neg_T24due.csv", stringsAsFactors = F)
meandelta_T24dueoos= read.csv("T24dueResults_Alberto/meandelta_T24dueoos.csv", stringsAsFactors = F)
meandelta_T24due= read.csv("T24dueResults_Alberto/meandelta_T24due.csv", stringsAsFactors = F)
meandeltau_T24dueoos= read.csv("T24dueResults_Alberto/meandeltau_T24dueoos.csv", stringsAsFactors = F)
meandeltau_T24due= read.csv("T24dueResults_Alberto/meandeltau_T24due.csv", stringsAsFactors = F)


#(1+120*2):(nw120+120)], y = meanh_T120[1,1:(nw120-window)

#pdf('delta24oos.pdf')
ylim = c(min(meandelta_T24dueoos[1,1:(nw24-window)]),0.3*max(meandelta_T24dueoos[1,1:(nw24-window)]))
#ylim = c(0,1)
#name = paste(folder_6, "PP_lambda", ".png", sep = "")
#png(name, width = 800, height = 500)
plot(x = ts.enddate[(1+24*2):(nw24+24)], y = meandelta_T24due[1,1:(nw24-window)],col="black",lwd=2, type = "l", main = "Pricing Performance of Risk Premia: \nConditional  (T=24)   \n In-Sample vs.  Out-of-Sample  ", xlab = "Date", ylab = "Test Pricing Errors", ylim = ylim)
abline(v = rec[which(rec > 0)], col = "grey80")
lines(x = ts.enddate[(1+24*2):(nw24+24)], y =  testdelta_pos_T24due[1,1:(nw24-window)], lty=1,lwd=1,col=2)
lines(x = ts.enddate[(1+24*2):(nw24+24)], y =  testdelta_neg_T24due[1,1:(nw24-window)], lty=1,lwd=1,col=2)
lines(x = ts.enddate[(1+24*2):(nw24+24)], y =meandelta_T24dueoos[1,1:(nw24-window)], lty=1,lwd=1,col=5)
legend(6000,0.0015,c("in-sample","CI+","CI-", "out-of-sample",   "econ-fin crises"), lwd=c(1.5,1.5), col=c(1,2,2,5,"grey80"), y.intersp=0.5)
dev.off()

apply(meandelta_T24due[,1:(nw24-window)],1,mean)
apply(meandelta_T24dueoos[,1:(nw24-window)],1,mean)
apply(meandeltau_T24due[,1:(nw24-window)],1,mean)
apply(meandeltau_T24dueoos[,1:(nw24-window)],1,mean)



testdelta_pos_T24due= read.csv("T24dueResults_Alberto/ PP.testdelta_pos_T24due.csv", stringsAsFactors = F)
testdelta_neg_T24due= read.csv("T24dueResults_Alberto/ PP.testdelta_neg_T24due.csv", stringsAsFactors = F)
meandelta_T24dueoos= read.csv("T24dueResults_Alberto/meandelta_T24dueoos.csv", stringsAsFactors = F)
meandelta_T24due= read.csv("T24dueResults_Alberto/meandelta_T24due.csv", stringsAsFactors = F)
meandeltau_T24dueoos= read.csv("T24dueResults_Alberto/meandeltau_T24dueoos.csv", stringsAsFactors = F)
meandeltau_T24due= read.csv("T24dueResults_Alberto/meandeltau_T24due.csv", stringsAsFactors = F)


apply(meandelta_T24due[,43:409],1,mean)
apply(meandelta_T24dueoos[,43:409],1,mean)
apply(meandeltau_T24due[,43:409],1,mean)
apply(meandeltau_T24dueoos[,43:409],1,mean)



#pdf('delta24oos.pdf')
ylim = c(min(meandelta_T24due[1,1:(nw24-window)]),max(meandelta_T24dueoos[1,1:(nw24-window)]))
#ylim = c(0,1)
#name = paste(folder_6, "PP_lambda", ".png", sep = "")
#png(name, width = 800, height = 500)
plot(x = ts.enddate[(1+24*2):(nw24+24)], y = meandelta_T24due[1,1:(nw24-window)],col="black",lwd=2, type = "l", main = "Pricing Performance of Risk Premia: \nConditional  (T=24)  \n In-Sample vs.  Out-of-Sample ", xlab = "Date", ylab = "Test Pricing Errors", ylim = ylim)
abline(v = rec[which(rec > 0)], col = "grey80")
lines(x = ts.enddate[(1+24*2):(nw24+24)], y =  testdelta_pos_T24due[1,1:(nw24-window)], lty=1,lwd=1,col=2)
lines(x = ts.enddate[(1+24*2):(nw24+24)], y =  testdelta_neg_T24due[1,1:(nw24-window)], lty=1,lwd=1,col=2)
lines(x = ts.enddate[(1+24*2):(nw24+24)], y =meandelta_T24dueoos[1,1:(nw24-window)], lty=1,lwd=1,col=5)
legend(17000,15e-05,c("in-sample","CI+","CI-", "out-of-sample",   "econ-fin crises"), lwd=c(1.5,1.5), col=c(1,2,2,5,"grey80"), y.intersp=0.5)
dev.off()




# 
# ylim = c(min(PP.h.nodemean[1,43:601]), max(PP.h.nodemean_oos[1,43:601]))
# #ylim = c(0,1)
# #name = paste(folder_6, "PP_lambda", ".png", sep = "")
# #png(name, width = 800, height = 500)
# plot(x = ts.enddate[43:601], y = PP.h.nodemean[1,43:601],col="black",lwd=2, type = "l", main = "Pricing Performance of SDF  \nConditional vs. Unconditional vs. FF5    (T=24)", xlab = "Date", ylab = "Test Pricing Errors", ylim = ylim)
# abline(v = rec[which(rec > 0)], col = "grey80")
# lines(x = ts.enddate[43:601], y =PP.h.nodemean_oos[1,43:601], lty=1,lwd=1,col=3)
# 
#     apply(PP.h[,43:601],1,mean)
#     apply(PP.h_oos[,43:601],1,mean)
#     apply(PP.hu_oos[,43:601],1,mean)
#     apply(PP.hu[,43:601],1,mean)
#     
#     
#     
#     ylim = c(min(PP.h[1,43:601]),max(PP.h_oos[1,43:601]))
#     #ylim = c(0,1)
#     #name = paste(folder_6, "PP_lambda", ".png", sep = "")
#     #png(name, width = 800, height = 500)
#     plot(x = ts.enddate[43:601], y = PP.h[1,43:601],col="black",lwd=2, type = "l", main = "Pricing Performance of SDF  \nConditional vs. Unconditional vs. FF5    (T=24)", xlab = "Date", ylab = "Test Pricing Errors", ylim = ylim)
#     abline(v = rec[which(rec > 0)], col = "grey80")
#     lines(x = ts.enddate[43:601], y =PP.h_oos[1,43:601], lty=1,lwd=1,col=3)
#     lines(x = ts.enddate[43:601], y =PP.hu_oos[1,43:601], lty=1,lwd=1,col=2)
#     lines(x = ts.enddate[43:601], y =PP.hu[1,43:601], lty=1,lwd=1,col=4)
#    
#      apply(PP.h[,43:601],1,mean)
#      apply(PP.hu[,43:601],1,mean)
#      
#      apply(PP.h_oos[,43:601],1,mean)
#     apply(PP.hu_oos[,43:601],1,mean)
#     
#    
#     
#     ylim = c(min(PP.h.nodemean[1,43:601]),max(PP.h.nodemean_oos[1,43:601]))
#     #ylim = c(0,1)
#     #name = paste(folder_6, "PP_lambda", ".png", sep = "")
#     #png(name, width = 800, height = 500)
#     plot(x = ts.enddate[43:601], y = PP.h.nodemean[1,43:601],col="black",lwd=2, type = "l", main = "Pricing Performance of SDF  \nConditional vs. Unconditional vs. FF5    (T=24)", xlab = "Date", ylab = "Test Pricing Errors", ylim = ylim)
#     abline(v = rec[which(rec > 0)], col = "grey80")
#     lines(x = ts.enddate[43:601], y =PP.h.nodemean_oos[1,43:601], lty=1,lwd=1,col=3)
#     lines(x = ts.enddate[43:601], y =PP.hu.nodemean_oos[1,43:601], lty=1,lwd=1,col=2)
#     lines(x = ts.enddate[43:601], y =PP.hu.nodemean[1,43:601], lty=1,lwd=1,col=4)
#     
#  
#     
#     ylim = c(min(PP.lambda[1,43:601]),max(PP.lambda_oos[1,43:601]))
#     #ylim = c(0,1)
#     #name = paste(folder_6, "PP_lambda", ".png", sep = "")
#     #png(name, width = 800, height = 500)
#     plot(x = ts.enddate[43:601], y = PP.lambda[1,43:601],col="black",lwd=2, type = "l", main = "Pricing Performance of SDF  \nConditional vs. Unconditional vs. FF5    (T=24)", xlab = "Date", ylab = "Test Pricing Errors", ylim = ylim)
#     abline(v = rec[which(rec > 0)], col = "grey80")
#     lines(x = ts.enddate[43:601], y =PP.lambda_oos[1,43:601], lty=1,lwd=1,col=3)
#     lines(x = ts.enddate[43:601], y =PP.lambdau_oos[1,43:601], lty=1,lwd=1,col=2)
#     lines(x = ts.enddate[43:601], y =PP.lambdau[1,43:601], lty=1,lwd=1,col=4)
#     
#     apply(PP.lambda[,43:601],1,mean)
#     apply(PP.lambdau[,43:601],1,mean)
#     
#     apply(PP.lambda_oos[,43:601],1,mean)
#     apply(PP.lambdau_oos[,43:601],1,mean)
#     
#     
#     
#     ylim = c(min(PP.lambda.nodemean[1,43:601]),max(PP.lambda.nodemean_oos[1,43:601]))
#     #ylim = c(0,1)
#     #name = paste(folder_6, "PP_lambda", ".png", sep = "")
#     #png(name, width = 800, height = 500)
#     plot(x = ts.enddate[43:601], y = PP.lambda.nodemean[1,43:601],col="black",lwd=2, type = "l", main = "Pricing Performance of SDF  \nConditional vs. Unconditional vs. FF5    (T=24)", xlab = "Date", ylab = "Test Pricing Errors", ylim = ylim)
#     abline(v = rec[which(rec > 0)], col = "grey80")
#     lines(x = ts.enddate[43:601], y =PP.lambda.nodemean_oos[1,43:601], lty=1,lwd=1,col=3)
#     lines(x = ts.enddate[43:601], y =PP.lambdau.nodemean_oos[1,43:601], lty=1,lwd=1,col=2)
#     lines(x = ts.enddate[43:601], y =PP.lambdau.nodemean[1,43:601], lty=1,lwd=1,col=4)
#     
#   
#     
#       ylim = c(min(SS_cond_oos[1,43:540]),max(SS_cond_oos[1,43:540]))
#       #ylim = c(0,1)
#       #name = paste(folder_6, "PP_lambda", ".png", sep = "")
#       #png(name, width = 800, height = 500)
#       plot(x = ts.enddate[43:540], y = SS_cond_oos[1,43:540],col="black",lwd=2, type = "l", main = "Pricing Performance of SDF  \nConditional vs. Unconditional vs. FF5    (T=24)", xlab = "Date", ylab = "Test Pricing Errors", ylim = ylim)
#       abline(v = rec[which(rec > 0)], col = "grey80")
#     lines(x = ts.enddate[43:540], y =SS_uncond_oos[1,43:540], lty=1,lwd=1,col=3)
#     lines(x = ts.enddate[43:540], y =SS_cond[43:540,1], lty=1,lwd=1,col=2)
#     lines(x = ts.enddate[43:540], y =SS_uncond[43:540,1], lty=1,lwd=1,col=4)
#     lines(x = ts.enddate[43:540], y =0.1*SS_condFF_oos[1,43:540], lty=1,lwd=1,col=5)
#     
#   
#     apply(SS_cond_oos[,43:540],1,mean)/apply(SS_uncond_oos[,43:540],1,mean)
#     
#     mean(SS_cond[43:540,])/mean(SS_uncond[43:540,])
#     
#   
#     apply(mean_cond_oos[,43:540],1,mean)/apply(mean_uncond_oos[,43:540],1,mean)
#     
#     mean(mean_cond[43:540,])/mean(mean_uncond[43:540,])
#     
#     
#     apply(mean_acond_oos[,43:540],1,mean)/apply(mean_auncond_oos[,43:540],1,mean)
#     
#     mean(mean_acond[43:540,])/mean(mean_auncond[43:540,])
#     
#       
#     
#     ylim = c(min(mean_cond_oos[1,43:540]),max(mean_cond_oos[1,43:540]))
#     #ylim = c(0,1)
#     #name = paste(folder_6, "PP_lambda", ".png", sep = "")
#     #png(name, width = 800, height = 500)
#     plot(x = ts.enddate[43:540], y = mean_cond_oos[1,43:540],col="black",lwd=2, type = "l", main = "Pricing Performance of SDF  \nConditional vs. Unconditional vs. FF5    (T=24)", xlab = "Date", ylab = "Test Pricing Errors", ylim = ylim)
#     abline(v = rec[which(rec > 0)], col = "grey80")
#     lines(x = ts.enddate[43:540], y =mean_uncond_oos[1,43:540], lty=1,lwd=1,col=3)
#     lines(x = ts.enddate[43:540], y =mean_cond[43:540,1], lty=1,lwd=1,col=2)
#     lines(x = ts.enddate[43:540], y =mean_uncond[43:540,1], lty=1,lwd=1,col=4)
#     lines(x = ts.enddate[43:540], y=mean_condFF_oos[1,43:540], lty=1,lwd=1,col=5)
#     
#     
#     
#     ylim = c(min(mean_acond_oos[1,43:540]),max(mean_acond_oos[1,43:540]))
#     #ylim = c(0,1)
#     #name = paste(folder_6, "PP_lambda", ".png", sep = "")
#     #png(name, width = 800, height = 500)
#     plot(x = ts.enddate[43:540], y = mean_acond_oos[1,43:540],col="black",lwd=2, type = "l", main = "Pricing Performance of SDF  \nConditional vs. Unconditional vs. FF5    (T=24)", xlab = "Date", ylab = "Test Pricing Errors", ylim = ylim)
#     abline(v = rec[which(rec > 0)], col = "grey80")
#     lines(x = ts.enddate[43:540], y =mean_auncond_oos[1,43:540], lty=1,lwd=1,col=3)
#     lines(x = ts.enddate[43:540], y =mean_acond[43:540,1], lty=1,lwd=1,col=2)
#     lines(x = ts.enddate[43:540], y =mean_auncond[43:540,1], lty=1,lwd=1,col=4)
#     lines(x = ts.enddate[43:540], y=mean_acondFF_oos[1,43:540], lty=1,lwd=1,col=5)
    
    
    
    
    
       
    #end of oos
    
    
    aaa=seq(1:NN)
    plot(aaa,totdeltaFF[,1],col=4)
    lines(x=aaa,y=totdeltau[,1],col=2)
    lines(x=aaa,y=totdelta[,1],col=3)
    totX=as.matrix(totX)
    

#    sss <- data.frame(cbind(totX,totLamPrem,totLamPremFF,totLamPremFFu,totLamPremu))
        sss <- data.frame(cbind(totX,totLamPrem,totLamPremFF,totLamPremu))
    
    #http://r-statistics.co/Top50-Ggplot2-Visualizations-MasterList-R-Code.html
    #http://r-statistics.co/ggplot2-Tutorial-With-R.html
 ###   pdf('45degree24.pdf')  
# #   ggplot(sss) + geom_point(aes(x=totLamPrem[,1], y=totX, color=3)) +geom_smooth(aes(x=totLamPrem[,1], y=totX, color=4))+geom_abline(mapping = NULL,data = NULL, slope=1, intercept=0, na.rm = FALSE,show.legend = NA)
#    gg<-ggplot(sss) + geom_point(aes(x=totLamPrem[,1], y=totX, colour=3))+ 
        gg<-ggplot(sss) + geom_point(aes(x=totLamPrem[,1], y=totX))+ 
    
    #      # geom_point(aes(x=totLamPremu[,1], y=totX, color=5)) +
#      # geom_point(aes(x=totLamPremFF[,1], y=totX, colour=6)) +
    geom_abline(mapping = NULL,data = NULL, slope=1, intercept=0, na.rm = FALSE, show.legend = F, col="blue" , lwd=3 , lty=2)+
    geom_smooth(method = "lm", aes(x=totLamPrem[,1], y=totX, colour="cond PCA"))+
    geom_smooth(method = "lm", aes(x=totLamPremu[,1], y=totX, colour="uncon PCA"))+
    geom_smooth(method = "lm", aes(x=totLamPremFF[,1], y=totX, colour="cond FF5"))+
          scale_colour_manual(name="models",values=c("blue","green", "red"))
     gg+ xlab("estimated expected excess return") +
       labs(title="Sample Returns vs. Model-Implied Expected Returns  (T=24)")+
 ylab("sample excess return")
     dev.off()    
     
        # gg+   scale_colour_manual("", 
        #                     breaks = c("AAB", "AA1", "AA3", "AA2"),
        #                     values = c("AAB"="blue","AA1"="black", "AA3"="green", 
        #                                "AA2"="red")) 
       #   xlab(" ") +
      #    scale_y_continuous("Temperatura (C)", limits = c(-10,40)) + 
      #    labs(title="TITULO")
        
        
#      gg  + ggtitle()
        # gg+ scale_fill_discrete(name = "New Legend Title")
      #labs(fill = "Dose (mg)")
   #   gg+legend(9000,-0.00002,c("cond","CI+","CI-", "uncond", "cond FF5" , "fin crises"), lwd=c(1.5,1.5), col=c(1,2,2,3,4,"grey80"), y.intersp=0.5)

      
  #  print(gg)
    
    cor(totLamPrem[,1],totX)
    cor(totLamPremu[,1],totX)
    cor(totLamPremFF[,1],totX)
    cor(totLamPremFFu[,1],totX)
    
    #oos
    
    totX_oos=as.matrix(totX_oos)
    
    sss_oos <- data.frame(cbind(totX,totLamPrem,totLamPremFF,totLamPremu))
    
    #http://r-statistics.co/Top50-Ggplot2-Visualizations-MasterList-R-Code.html
    #http://r-statistics.co/ggplot2-Tutorial-With-R.html
    ###pdf('45degreeT120oos.pdf')  
    # #   ggplot(sss) + geom_point(aes(x=totLamPrem[,1], y=totX, color=3)) +geom_smooth(aes(x=totLamPrem[,1], y=totX, color=4))+geom_abline(mapping = NULL,data = NULL, slope=1, intercept=0, na.rm = FALSE,show.legend = NA)
    #    gg<-ggplot(sss) + geom_point(aes(x=totLamPrem[,1], y=totX, colour=3))+ 
    gg_oos<-ggplot(sss_oos) + geom_point(aes(x=totLamPrem[,1], y=totX))+ 
      
      #      # geom_point(aes(x=totLamPremu[,1], y=totX, color=5)) +
      #      # geom_point(aes(x=totLamPremFF[,1], y=totX, colour=6)) +
      geom_abline(mapping = NULL,data = NULL, slope=1, intercept=0, na.rm = FALSE, show.legend = F, col="blue" , lwd=3 , lty=2)+
      geom_smooth(method = "lm", aes(x=totLamPrem[,1], y=totX, colour="cond PCA"))+
      geom_smooth(method = "lm", aes(x=totLamPremu[,1], y=totX, colour="uncon PCA"))+
      geom_smooth(method = "lm", aes(x=totLamPremFF[,1], y=totX, colour="cond FF5"))+
      scale_colour_manual(name="models",values=c("blue","green", "red"))
    gg_oos+ xlab("estimated expected excess return") +
      labs(title="Sample Returns vs. Model-Implied Expected Returns  (T=24)")+
      ylab("sample excess return")
    dev.off()    
    
    # gg+   scale_colour_manual("", 
    #                     breaks = c("AAB", "AA1", "AA3", "AA2"),
    #                     values = c("AAB"="blue","AA1"="black", "AA3"="green", 
    #                                "AA2"="red")) 
    #   xlab(" ") +
    #    scale_y_continuous("Temperatura (C)", limits = c(-10,40)) + 
    #    labs(title="TITULO")
    
    
    #      gg  + ggtitle()
    # gg+ scale_fill_discrete(name = "New Legend Title")
    #labs(fill = "Dose (mg)")
    #   gg+legend(9000,-0.00002,c("cond","CI+","CI-", "uncond", "cond FF5" , "fin crises"), lwd=c(1.5,1.5), col=c(1,2,2,3,4,"grey80"), y.intersp=0.5)
    
    
    #  print(gg)
    
    cor(totLamPrem[,1],totX_oos)
    cor(totLamPremu[,1],totX_oos)
    cor(totLamPremFF[,1],totX_oos)
    cor(totLamPremFFu[,1],totX_oos)
    
    #end of oos
      
       
    
    # df <- data.frame(c("cond","uncond"))
    # gg+facet_wrap(  ~ sss, ncol=3)
    # 
    # geom_hline(
    #   mapping = NULL,
    #   data = NULL,
    #   ...,
    #   yintercept,
    #   na.rm = FALSE,
    #   show.legend = NA
    # )
    # 
    # geom_vline(
    #   mapping = NULL,
    #   data = NULL,
    #   ...,
    #   xintercept,
    #   na.rm = FALSE,
    #   show.legend = NA
    # )
    # 
    
    
    # PE from ER

    
    ylim = c(min(PP.h[1,43:(nw-43)]),max(PP.h[1,43:(nw-43)]))
    #plot(x = ts.enddate, y = sdfT, type = "l", main = "Pricing performance PCA", xlab = "Date", ylab = "Pricing Performance", ylim = ylim)
    plot(x = ts.enddate[43:(nw-43)], y =matrix(PP.h[1,43:(nw-43)],ncol=1), type = "l", main = "Pricing performance PCA", xlab = "Date", ylab = "Pricing Performance", ylim = ylim)
    lines(x  = ts.enddate[43:(nw-43)], y =  PP.testh_pos[1,43:(nw-43)] ,col=2)
    lines(x  = ts.enddate[43:(nw-43)], y =  PP.testh_neg[1,43:(nw-43)] ,col=3)
    
    #   lines(x  = ts.enddate[43:nw], y =  PP.h[2,43:nw] ,col=4)
    #  lines(x  = ts.enddate[43:nw], y =  PP.h[3,43:nw] ,col=5)
    #  lines(x  = ts.enddate[43:nw], y =  PP.h[4,43:nw] ,col=6)
    # # 
    ## for (i in c(2:nrow(PP.lambda), 1)) lines(x = ts.enddate, y = PP.lambda[i,], col = i)
    lines(x  = ts.enddate[43:(nw-43)], y =  PP.hu[1,43:(nw-43)] ,col=4)
    

    
    ylim = c(min(PP.h[1,43:(nw-43)]),max(PP.h.nodemean[1,43:(nw-43)]))
    
    #ylim = c(min(PP.h.nodemean[1,43:nw]),max(PP.h.nodemean[43:nw]))
    #plot(x = ts.enddate, y = sdfT, type = "l", main = "Pricing performance PCA", xlab = "Date", ylab = "Pricing Performance", ylim = ylim)
    plot(x = ts.enddate[43:(nw-43)], y =PP.h.nodemean[1,43:(nw-43)], type = "l", main = "Pricing performance PCA", xlab = "Date", ylab = "Pricing Performance", ylim = ylim)
    #lines(x  = ts.enddate[43:nw], y =  PP.h.nodemeanFF2[43:nw] ,col=5)
    
    
    lines(x  = ts.enddate[43:nw], y = matrix(PP.hu.nodemean[1,43:nw],ncol=1),col=2)

    # cc=PP.h.nodemeanFF[1,]<10*max(PP.h.nodemean[1,43:nw])
    # cc=1*cc
    # PP.h.nodemeanFF2=cc*PP.h.nodemeanFF[1,]
    # #  filter( PP.h.nodemeanFF[1,] <= 10) %>% 
    
    # lines(x  = ts.enddate[43:nw], y =  PP.h[2,43:nw] ,col=2)
    # lines(x  = ts.enddate[43:nw], y =  PP.h[3,43:nw] ,col=3)
    # lines(x  = ts.enddate[43:nw], y =  PP.h[4,43:nw] ,col=4)
    # 
    ## for (i in c(2:nrow(PP.lambda), 1)) lines(x = ts.enddate, y = PP.lambda[i,], col = i)
    #lines(x  = ts.enddate[43:nw], y =  PP.hu[1,43:nw] ,col=4)
    
    
    
    
    
    ylim = c(min(PP.lambda[1,43:nw]),max(PP.lambdau[1,43:nw]))
    #plot(x = ts.enddate, y = sdfT, type = "l", main = "Pricing performance PCA", xlab = "Date", ylab = "Pricing Performance", ylim = ylim)
    plot(x = ts.enddate[43:nw], y =matrix(PP.lambda[1,43:nw],ncol=1), type = "l", main = "Pricing performance PCA", xlab = "Date", ylab = "Pricing Performance", ylim = ylim)
    
    ## for (i in c(2:nrow(PP.lambda), 1)) lines(x = ts.enddate, y = PP.lambda[i,], col = i)
    lines(x  = ts.enddate[43:nw], y =  PP.testdelta_neg[1,43:nw] ,col=3)
    lines(x  = ts.enddate[43:nw], y =  PP.testdelta_pos[1,43:nw] ,col=2)
    lines(x  = ts.enddate[43:nw], y =  PP.lambdau[1,43:nw],col=4)
    
    
   
    
    ylim = c(min(PP.lambda[1,43:nw]),max(PP.lambdaFF[1,43:nw]))
    #plot(x = ts.enddate, y = sdfT, type = "l", main = "Pricing performance PCA", xlab = "Date", ylab = "Pricing Performance", ylim = ylim)
    plot(x = ts.enddate[43:nw], y =matrix(PP.lambda[1,43:nw],ncol=1), type = "l", main = "Pricing performance PCA", xlab = "Date", ylab = "Pricing Performance", ylim = ylim)
    
    ## for (i in c(2:nrow(PP.lambda), 1)) lines(x = ts.enddate, y = PP.lambda[i,], col = i)
    lines(x  = ts.enddate[43:nw], y = PP.lambdaFF[43:nw] ,col=3)
    
    
    ylim = c(min(PP.lambda[1,43:nw]),max(PP.lambda[1,43:nw]))
    #plot(x = ts.enddate, y = sdfT, type = "l", main = "Pricing performance PCA", xlab = "Date", ylab = "Pricing Performance", ylim = ylim)
    plot(x = ts.enddate[43:nw], y =matrix(PP.lambda[1,43:nw],ncol=1), type = "l", main = "Pricing performance PCA", xlab = "Date", ylab = "Pricing Performance", ylim = ylim)
    
    ## for (i in c(2:nrow(PP.lambda), 1)) lines(x = ts.enddate, y = PP.lambda[i,], col = i)
    lines(x  = ts.enddate[43:nw], y = PP.lambdau[1,43:nw] ,col=3)
    
    
    
    ylim = c(min(PP.lambda.nodemean[1,43:nw]),max(PP.lambdaFF.nodemean[43:nw]))
    #plot(x = ts.enddate, y = sdfT, type = "l", main = "Pricing performance PCA", xlab = "Date", ylab = "Pricing Performance", ylim = ylim)
    plot(x = ts.enddate[43:nw], y =matrix(PP.lambda.nodemean[1,43:nw],ncol=1), type = "l", main = "Pricing performance PCA", xlab = "Date", ylab = "Pricing Performance", ylim = ylim)
    
    ## for (i in c(2:nrow(PP.lambda), 1)) lines(x = ts.enddate, y = PP.lambda[i,], col = i)
    lines(x  = ts.enddate[43:nw], y = PP.lambdaFF.nodemean[43:nw] ,col=3)
    
    ylim = c(min(PP.lambdauFF[43:nw]),max(PP.lambdauFF[43:nw]))
    #plot(x = ts.enddate, y = sdfT, type = "l", main = "Pricing performance PCA", xlab = "Date", ylab = "Pricing Performance", ylim = ylim)
    plot(x = ts.enddate[43:nw], y =matrix(PP.lambdau[1,43:nw],ncol=1), type = "l", main = "Pricing performance PCA", xlab = "Date", ylab = "Pricing Performance", ylim = ylim)
    
    ## for (i in c(2:nrow(PP.lambda), 1)) lines(x = ts.enddate, y = PP.lambda[i,], col = i)
    lines(x  = ts.enddate[43:nw], y = PP.lambdauFF[43:nw] ,col=3)
    
    ylim = c(min(PP.lambdauFF.nodemean[43:nw]),max(PP.lambdauFF.nodemean[43:nw]))
    #plot(x = ts.enddate, y = sdfT, type = "l", main = "Pricing performance PCA", xlab = "Date", ylab = "Pricing Performance", ylim = ylim)
    plot(x = ts.enddate[43:nw], y =matrix(PP.lambdau.nodemean[1,43:nw],ncol=1), type = "l", main = "Pricing performance PCA", xlab = "Date", ylab = "Pricing Performance", ylim = ylim)
    
    ## for (i in c(2:nrow(PP.lambda), 1)) lines(x = ts.enddate, y = PP.lambda[i,], col = i)
    lines(x  = ts.enddate[43:nw], y = PP.lambdauFF.nodemean[43:nw] ,col=3)
    
    
    ylim = c(min(PP.testdelta[1,43:nw]),max(PP.testdelta[1,43:nw]))
    #plot(x = ts.enddate, y = sdfT, type = "l", main = "Pricing performance PCA", xlab = "Date", ylab = "Pricing Performance", ylim = ylim)
    plot(x = ts.enddate[43:nw], y =matrix(PP.testdelta[1,43:nw],ncol=1), type = "l", main = "Pricing performance PCA", xlab = "Date", ylab = "Pricing Performance", ylim = ylim)
    
    ## for (i in c(2:nrow(PP.lambda), 1)) lines(x = ts.enddate, y = PP.lambda[i,], col = i)
    lines(x  = ts.enddate[43:nw], y = PP.testdelta2[1,43:nw] ,col=3)
    
    
    
    
    # PE from SDF
    
    ylim = c(min(SS_uncond[43:nw]),max(SS_uncond[43:nw]))
    #plot(x = ts.enddate, y = sdfT, type = "l", main = "Pricing performance PCA", xlab = "Date", ylab = "Pricing Performance", ylim = ylim)
    plot(x = ts.enddate[43:nw], y = SS_cond[43:nw], type = "l", main = "Pricing performance PCA", xlab = "Date", ylab = "Pricing Performance", ylim = ylim)
    
    ## for (i in c(2:nrow(PP.lambda), 1)) lines(x = ts.enddate, y = PP.lambda[i,], col = i)
    lines(x  = ts.enddate[43:nw], y = SS_uncond[43:nw] ,col=3)
    
    
    ylim = c(min(SS_uncond[43:nw]),max(SS_uncond[43:nw]))
    #plot(x = ts.enddate, y = sdfT, type = "l", main = "Pricing performance PCA", xlab = "Date", ylab = "Pricing Performance", ylim = ylim)
    plot(x = ts.enddate[43:nw], y = SS_cond[43:nw], type = "l", main = "Pricing performance PCA", xlab = "Date", ylab = "Pricing Performance", ylim = ylim)
    
    ## for (i in c(2:nrow(PP.lambda), 1)) lines(x = ts.enddate, y = PP.lambda[i,], col = i)
    lines(x  = ts.enddate[43:nw], y = SS_condFF[43:nw] ,col=3)
    
    ylim = c(min(SS_uncond[43:nw]),max(SS_uncond[43:nw]))
    #plot(x = ts.enddate, y = sdfT, type = "l", main = "Pricing performance PCA", xlab = "Date", ylab = "Pricing Performance", ylim = ylim)
    plot(x = ts.enddate[43:nw], y = SS_uncond[43:nw], type = "l", main = "Pricing performance PCA", xlab = "Date", ylab = "Pricing Performance", ylim = ylim)
    
    ## for (i in c(2:nrow(PP.lambda), 1)) lines(x = ts.enddate, y = PP.lambda[i,], col = i)
    lines(x  = ts.enddate[43:nw], y = SS_uncondFF[43:nw] ,col=3)
    
    
    
    ylim = c(min(mean_uncond[43:nw]),max(mean_cond[43:(nw-50)]))
    #plot(x = ts.enddate, y = sdfT, type = "l", main = "Pricing performance PCA", xlab = "Date", ylab = "Pricing Performance", ylim = ylim)
    plot(x = ts.enddate[43:nw], y = mean_cond[43:nw], type = "l", main = "Pricing performance PCA", xlab = "Date", ylab = "Pricing Performance", ylim = ylim)
    
    ## for (i in c(2:nrow(PP.lambda), 1)) lines(x = ts.enddate, y = PP.lambda[i,], col = i)
    lines(x  = ts.enddate[43:nw], y = mean_uncond[43:nw] ,col=3)
    
    
    ylim = c(min(mean_uncond[43:nw]),max(mean_uncond[43:nw]))
    #plot(x = ts.enddate, y = sdfT, type = "l", main = "Pricing performance PCA", xlab = "Date", ylab = "Pricing Performance", ylim = ylim)
    plot(x = ts.enddate[43:nw], y = mean_cond[43:nw], type = "l", main = "Pricing performance PCA", xlab = "Date", ylab = "Pricing Performance", ylim = ylim)
    
    ## for (i in c(2:nrow(PP.lambda), 1)) lines(x = ts.enddate, y = PP.lambda[i,], col = i)
    lines(x  = ts.enddate[43:nw], y = mean_condFF[43:nw] ,col=3)
    
    
    ylim = c(min(mean_uncond[43:nw]),max(mean_uncond[43:nw]))
    #plot(x = ts.enddate, y = sdfT, type = "l", main = "Pricing performance PCA", xlab = "Date", ylab = "Pricing Performance", ylim = ylim)
    plot(x = ts.enddate[43:nw], y = mean_uncond[43:nw], type = "l", main = "Pricing performance PCA", xlab = "Date", ylab = "Pricing Performance", ylim = ylim)
    
    ## for (i in c(2:nrow(PP.lambda), 1)) lines(x = ts.enddate, y = PP.lambda[i,], col = i)
    lines(x  = ts.enddate[43:nw], y = mean_uncondFF[43:nw] ,col=3)
    
    
    
    
    ylim = c(min(mean_auncond[43:nw]),max(mean_auncond[43:nw]))
    #plot(x = ts.enddate, y = sdfT, type = "l", main = "Pricing performance PCA", xlab = "Date", ylab = "Pricing Performance", ylim = ylim)
    plot(x = ts.enddate[43:nw], y = mean_acond[43:nw], type = "l", main = "Pricing performance PCA", xlab = "Date", ylab = "Pricing Performance", ylim = ylim)
    
    ## for (i in c(2:nrow(PP.lambda), 1)) lines(x = ts.enddate, y = PP.lambda[i,], col = i)
    lines(x  = ts.enddate[43:nw], y = mean_auncond[43:nw] ,col=3)
    
    
    
    ylim = c(min(mean_auncond[43:nw]),max(mean_auncond[43:nw]))
    #plot(x = ts.enddate, y = sdfT, type = "l", main = "Pricing performance PCA", xlab = "Date", ylab = "Pricing Performance", ylim = ylim)
    plot(x = ts.enddate[43:nw], y = mean_acond[43:nw], type = "l", main = "Pricing performance PCA", xlab = "Date", ylab = "Pricing Performance", ylim = ylim)
    
    ## for (i in c(2:nrow(PP.lambda), 1)) lines(x = ts.enddate, y = PP.lambda[i,], col = i)
    lines(x  = ts.enddate[43:nw], y = mean_acondFF[43:nw] ,col=3)
    
    
    
    
    ylim = c(min(mean_auncond[43:nw]),max(mean_auncond[43:nw]))
    #plot(x = ts.enddate, y = sdfT, type = "l", main = "Pricing performance PCA", xlab = "Date", ylab = "Pricing Performance", ylim = ylim)
    plot(x = ts.enddate[43:nw], y = mean_auncond[43:nw], type = "l", main = "Pricing performance PCA", xlab = "Date", ylab = "Pricing Performance", ylim = ylim)
    
    ## for (i in c(2:nrow(PP.lambda), 1)) lines(x = ts.enddate, y = PP.lambda[i,], col = i)
    lines(x  = ts.enddate[43:nw], y = mean_auncondFF[43:nw] ,col=3)
    
    print(Sys.time() - time)
    rm(i, j, index, X, N, X_mean, rf.supp, factor.supp, lambda.supp, risk_premia.supp, k, k0)
    rm(delta.lambda, delta.beta, reg, x, time)
  #}
}

  
# Out Of Sample Exercise
{
  # PORTFOLIO

  k0 =c(1, 2, 3, 5, 10)


  
  
  
  

  
  
  
  

  

  
  
  NN=ncol(data)
  lambdau.supp= matrix(NA, nrow = NN, ncol = 10)

  
  for (i in 1:NN)
  {
    X_i_full = data[,i] - (rf-1)             # vettore lungo 648 relativo all'asset i
    i_na = is.na(X_i_full)    # posizioni del vettore X_i_full dove ci sono valori mancanti
    X_i = X_i_full[!i_na]               # vettore esclusi i valori mancanti (le osservazioni potrebbero non essere consecutive)
    F_i =  ts.factorsu[!i_na,,1]                # factors ai soli timesteps dove sono presenti osservazioni per X_i
    lambdau.supp[i,] = t(X_i) %*% F_i / length(X_i)
  }
  
gammak= matrix(apply(ts.factorsu, 2, mean),ncol=1)
omegak=cov(matrix(ts.factorsu[,,1],ncol=10))



selectu = apply(data, 2, function(x){sum(is.na(x))})
selectu = (selectu == 0)
data_balanced = data[,selectu]


# PCA for balanced

XX=data_balanced#data[,qqq] # note that risk free is not taken away
TTT=nrow(data)
FPCAu = sqrt(TTT) * as.matrix(eigen(XX %*% t(XX))$vectors[,1:kmax])
lambdauPCA = t((1/TTT) * t(FPCAu) %*% XX)
valuefuPCA=matrix(0,10,1)
k=1;
for (k in 1:10)
{valuefuPCA[k]=(1/(ncol(data_balanced)*nrow(data)))*tr( (XX - FPCAu[,1:k]%*%t(lambdauPCA[,1:k]))%*%
                                                          t( XX - FPCAu[,1:k]%*%t(lambdauPCA[,1:k])   )     )
}




  

  nfactorhatu=1
  lambdau=lambdau.supp
  valuefu=ts.valuefu

    # FF
  NN=ncol(data)
  lambdau.suppFF= matrix(NA, nrow = NN, ncol = 5)
  
  
  for (i in 1:NN)
  {
    
    X_i_fullFF = data[,i]             # vettore lungo 648 relativo all'asset i
    i_naFF = is.na(X_i_fullFF)    # posizioni del vettore X_i_full dove ci sono valori mancanti
    X_iFF = X_i_fullFF[!i_naFF]               # vettore esclusi i valori mancanti (le osservazioni potrebbero non essere consecutive)
    FFF=FF[,]
    F_iFF =  FFF[!i_naFF,]                # factors ai soli timesteps dove sono presenti osservazioni per X_i
    summ=sum(!i_naFF)
    if (summ<6)
    {lambdau.suppFF[i,] = matrix(NA,1,5)}
    else
    {      aa=length(X_iFF)
    MM=(diag(1,aa,aa)-(1/aa)*matrix(1,aa,1)%*%matrix(1,1,aa)  )
    lambdau.suppFF[i,] = t(X_iFF) %*% MM %*% F_iFF %*% solve(t(F_iFF)%*% MM %*% F_iFF)}
    
  }
  

  
  p=0.05
  zp=qnorm(p)
  tar_vol=0.05/sqrt(12)
  
  
  
  
  risk_premiau.supp=  apply(ts.factorsu, 2, mean)
  omegau.supp=cov(matrix(ts.factorsu[,,1],ncol=10))
  
  gamu=matrix(NA,10,1)
  VaR_outvu_is2=matrix(NA,10,1)
  ES_outvu_is2=matrix(NA,10,1)
  
  
    j=1
  for (j in 1:10)
  {
  nfacuu=j
  gamu[j]=t(risk_premiau.supp[1:nfacuu])%*%omegau.supp[1:nfacuu,1:nfacuu]%*%risk_premiau.supp[1:nfacuu]
  VaR_outvu_is2[j]=gamu[j]*tar_vol  +zp*tar_vol
  ES_outvu_is2[j]=gamu[j]*tar_vol     -(dnorm(zp)/p)*tar_vol
  }
  cbind(sqrt(12)*gamu,VaR_outvu_is2,ES_outvu_is2)
  
  risk_premiauFF.supp=  apply(FF, 2, mean)
  omegauFF.supp=cov(FF)
  
  
  i_naLL = is.na(lambdau.suppFF)
  meanlamFF=100*apply(matrix(lambdau.suppFF[!i_naLL],ncol=5), 2, mean)
  
  covlamFF=100*100*cov(matrix(lambdau.suppFF[!i_naLL],ncol=5))
  
  
  # stop FF
  
  
 
  # preparation for pseudo oos 
  
#this is for unconditional IS and OOS over full sample 
  
  Thalf=0.5*(nrow(X_balanced))
  factoru_halfT = sqrt(Thalf) * as.matrix(eigen(X_balanced[1:Thalf,] %*% t(X_balanced[1:Thalf,]))$vectors[,1:10])
  riskpremiau_halfT=matrix(apply( factoru_halfT,2,mean),10,1)
  lambdau_halfT = t(1/Thalf *(t(factoru_halfT) %*% X_balanced[1:Thalf,]))



#10x1

omega = diag(1,10) - risk_premiau_Thalf %*% t(risk_premiau_Thalf)
mulambda= t(lambdau_halfT)%*%matrix(1,N_bal,1)/N_bal




rrr_unc_oos=matrix(NA,nrow=Thalf,ncol=10)
rrr_unc_oos2=matrix(NA,nrow=Thalf,ncol=10)
rrr_uncv_oos=matrix(NA,nrow=Thalf,ncol=10)
rrr_uncv_oos2=matrix(NA,nrow=Thalf,ncol=10)


j=1
for (j in 1:10)
{
  val=0
  val = 1/(N_bal*Thalf) * sum((X_balanced[1:Thalf,] -   factoru_halfT[,1:j] %*% t(lambdau_halfT[,1:j]))^2)
 sigma2=0
   sigma2=Thalf/(Thalf - j)*val
   sigmalambda=matrix(NA,j,j)
   sigmalambda1=matrix(NA,j,j)
   sigmalambda=(t(lambdau_halfT[,1:j]) %*% lambdau_halfT[,1:j]) - (N_bal/Thalf)*sigma2*diag(1,j)
sigmalambda1=(t(lambdau_halfT[,1:j]) %*% lambdau_halfT[,1:j])
w2 = solve(sigma2*solve(omega[1:j,1:j]) + sigmalambda)

aaa=( t(risk_premiau_Thalf[1:j])%*%solve(omega[1:j,1:j])%*%w2%*%sigmalambda%*%w2%*%solve(omega[1:j,1:j])%*%risk_premiau_Thalf[1:j])


if(aaa<=0)     { sigmalambda=sigmalambda1}      

w2=matrix(NA,nrow=j, ncol=j)

w2 = solve(sigma2*solve(omega[1:j,1:j]) + sigmalambda)
w22 =  lambdau_halfT[,1:j] %*% w2 %*% solve(omega[1:j,1:j])%*%risk_premiau_Thalf[1:j]

w_tan = w22/sum(w22)

gL= (w2%*%solve(omega[1:j,1:j])%*%risk_premiau_Thalf[1:j])/as.numeric(sum(w22)) 

rrr_unc_oos[,j]= X_balanced[(Thalf+1):TTT,]%*%w_tan - (N_bal/Thalf)*sigma2*factoru_halfT[,1:j]%*%gL

rrr_unc_oos2[,j]=X_balanced[(Thalf+1):TTT,]%*%w_tan

# target vol
if (j==1)
{ 
  volp=sqrt( t(risk_premiau_Thalf[1])%*%solve(omega[1,1])%*%w2%*%t(lambdau_halfT[,1])%*%
               (omega[1,1]*lambdau_halfT[,1]%*%t(lambdau_halfT[,1]) +sigma2*diag(N_bal))%*%lambdau_halfT[,1]%*%
               w2%*%solve(omega[1,1])%*%risk_premiau_Thalf[1])
  } else { volp=sqrt( t(risk_premiau_Thalf[1:j])%*%solve(omega[1:j,1:j])%*%w2%*%t(lambdau_halfT[,1:j])%*%
             (lambdau_halfT[,1:j]%*%omega[1:j,1:j]%*%t(lambdau_halfT[,1:j]) +sigma2*diag(N_bal))%*%lambdau_halfT[,1:j]%*%
             w2%*%solve(omega[1:j,1:j])%*%risk_premiau_Thalf[1:j])}
tar_vol=0.05/sqrt(12)
w_vol = (tar_vol)*w22%*%solve(volp)

gLv= tar_vol*(w2%*%solve(omega[1:j,1:j])%*%risk_premiau_Thalf[1:j])/as.numeric(volp)

rrr_uncv_oos[,j]=   X_balanced[(Thalf+1):TTT,]%*%w_vol - (N_bal/Thalf)*sigma2*factoru_halfT[,1:j]%*%gLv

rrr_uncv_oos2[,j]= X_balanced[(Thalf+1):TTT,]%*%w_vol

}


rrr_unc_is=matrix(NA,nrow=TTT,ncol=10)
rrr_unc_is2=matrix(NA,nrow=TTT,ncol=10)
rrr_uncv_is=matrix(NA,nrow=TTT,ncol=10)
rrr_uncv_is2=matrix(NA,nrow=TTT,ncol=10)

   omegau = diag(1,10) - apply(FPCAu,2,mean) %*% t(apply(FPCAu,2,mean))
   mulambdau= t(lambdauPCA)%*%matrix(1,N_bal,1)/N_bal
   riskpremiau=apply(FPCAu,2,mean)


j=1
for (j in 1:10)
{
  val=0
  val = 1/(N_bal*TTT) * sum((X_balanced -   FPCAu[,1:j] %*% t(lambdauPCA[,1:j]))^2)
  sigma2=0
  sigma2=TTT/(TTT - j)*val
  sigmalambda=matrix(NA,j,j)
  sigmalambda1=matrix(NA,j,j)
  sigmalambda=(t(lambdauPCA[,1:j]) %*% lambdauPCA[,1:j]) - (N_bal/TTT)*sigma2*diag(1,j)
  sigmalambda1=(t(lambdauPCA[,1:j]) %*% lambdauPCA[,1:j])
  w2 = solve(sigma2*solve(omegau[1:j,1:j]) + sigmalambda)
  
  aaa=( t(riskpremiau[1:j])%*%solve(omegau[1:j,1:j])%*%w2%*%sigmalambda%*%w2%*%solve(omegau[1:j,1:j])%*%riskpremiau[1:j])
  
  
  if(aaa<=0)     { sigmalambda=sigmalambda1}      
  
  w2=matrix(NA,nrow=j, ncol=j)
  
  w2 = solve(sigma2*solve(omegau[1:j,1:j]) + sigmalambda)
  w22 =  lambdauPCA[,1:j] %*% w2 %*% solve(omegau[1:j,1:j])%*%riskpremiau[1:j]
  w_tan = w22/sum(w22)

  gL= (w2%*%solve(omegau[1:j,1:j])%*%riskpremiau[1:j])/as.numeric(sum(w22)) 
  
  rrr_unc_is[,j]= X_balanced%*%w_tan - (N_bal/TTT)*sigma2*FPCAu[,1:j]%*%gL
  
  rrr_unc_is2[,j]=X_balanced%*%w_tan
  
  # target vol
  if (j==1)
  { 
    volp=sqrt( t(riskpremiau[1])%*%solve(omegau[1,1])%*%w2%*%t(lambdauPCA[,1])%*%
                 (omega[1,1]*lambdauPCA[,1]%*%t(lambdauPCA[,1]) +sigma2*diag(N_bal))%*%lambdauPCA[,1]%*%
                 w2%*%solve(omega[1,1])%*%riskpremiau[1])
  } else { volp=sqrt( t(riskpremiau[1:j])%*%solve(omegau[1:j,1:j])%*%w2%*%t(lambdauPCA[,1:j])%*%
                        (lambdauPCA[,1:j]%*%omegau[1:j,1:j]%*%t(lambdauPCA[,1:j]) +sigma2*diag(N_bal))%*%lambdauPCA[,1:j]%*%
                        w2%*%solve(omegau[1:j,1:j])%*%riskpremiau[1:j])}
  tar_vol=0.05/sqrt(12)
  w_vol = (tar_vol)*w22%*%solve(volp)

  gLv= tar_vol*(w2%*%solve(omegau[1:j,1:j])%*%riskpremiau[1:j])/as.numeric(volp)

  rrr_uncv_is[,j]=   X_balanced%*%w_vol - (N_bal/TTT)*sigma2*FPCAu[,1:j]%*%gLv
  
  rrr_uncv_is2[,j]= X_balanced%*%w_vol
  
}

SR_unc_is=sqrt(12)*apply(rrr_unc_is, 2, mean)/apply(rrr_unc_is, 2, sd)
SR_unc_is2=sqrt(12)*apply(rrr_unc_is2, 2, mean)/apply(rrr_unc_is2, 2, sd)
SR_uncv_is=sqrt(12)*apply(rrr_uncv_is, 2, mean)/apply(rrr_uncv_is, 2, sd)
SR_uncv_is2=sqrt(12)*apply(rrr_uncv_is2, 2, mean)/apply(rrr_uncv_is2, 2, sd)


SR_unc_oos=sqrt(12)*apply(rrr_unc_oos, 2, mean)/apply(rrr_unc_oos, 2, sd)
SR_unc_oos2=sqrt(12)*apply(rrr_unc_oos2, 2, mean)/apply(rrr_unc_oos2, 2, sd)
SR_uncv_oos=sqrt(12)*apply(rrr_uncv_oos, 2, mean)/apply(rrr_uncv_oos, 2, sd)
SR_uncv_oos2=sqrt(12)*apply(rrr_uncv_oos2, 2, mean)/apply(rrr_uncv_oos2, 2, sd)



    balancedN=selectu
     supp_new = parLapply(cl, 2:(nw - window-1), oos_parallel_new2, 
                         Fbal2v =  Fbal2v, fhalfcon2v = fhalfcon2v , Fabal2unbalv = Fabal2unbalv ,fhalfcon2unbalv = fhalfcon2unbalv , 
                         balancedN= balancedN , data = data, rf = rf, window = window, windowu=windowu, ts.eigenv=ts.eigenv , ts.deletestocks = ts.deletestocks, ts.factors = ts.factors, ts.valuef = ts.valuef, ts.nfactorhat = ts.nfactorhat, nfactorhatu = nfactorhatu , k0 = k0 , gammak = gammak , omegak=omegak , lambdau = lambdau , valuefu = valuefu , FF=FF , lambdau.suppFF = lambdau.suppFF , ts.sigma4 = ts.sigma4  )
    
    
    rrr_con_oosv=matrix(NA,nw,1)
    rrr_con_oos22v=matrix(NA,nw,1)
    rrr_conv_oosv=matrix(NA,nw,1)
    rrr_conv_oos2v=matrix(NA,nw,1)
    rrr_con_unbal_oosv=matrix(NA,nw,1)
    rrr_con_unbal_oos2v=matrix(NA,nw,1)
    rrr_conv_unbal_oosv=matrix(NA,nw,1)
    rrr_conv_unbal_oos2v=matrix(NA,nw,1)
    rrr_con_unbal2_oosv=matrix(NA,nw,1)
    rrr_con_unbal2_oos2v=matrix(NA,nw,1)
    rrr_conv_unbal2_oosv=matrix(NA,nw,1)
    rrr_conv_unbal2_oos2v=matrix(NA,nw,1)
  
    rrr_unc_oos1v=matrix(NA,nw,10)
    rrr_unc_oos2v=matrix(NA,nw,10)
    rrr_unc_oos3v=matrix(NA,nw,10)
    rrr_unc_oos4v=matrix(NA,nw,10)
    rrr_unc_oos5v=matrix(NA,nw,10)
    
    rrr_con_oos1v=matrix(NA,nw,10)
    rrr_con_oos2v=matrix(NA,nw,10)
    rrr_con_oos3v=matrix(NA,nw,10)
    rrr_con_oos4v=matrix(NA,nw,10)
    rrr_con_oos5v=matrix(NA,nw,10)
    rrr_con_oos3vdue=matrix(NA,nw,1)
    
    rrr_con_is1v=matrix(NA,nw,10)
    rrr_con_is2v=matrix(NA,nw,10)
    rrr_con_is3v=matrix(NA,nw,10)
    rrr_con_is4v=matrix(NA,nw,10)
    rrr_con_is5v=matrix(NA,nw,10)

    rrr_con_unbal_oos1v=matrix(NA,nw,10)
    rrr_con_unbal_oos2v=matrix(NA,nw,10)
    rrr_con_unbal_oos3v=matrix(NA,nw,10)
    rrr_con_unbal_oos4v=matrix(NA,nw,10)
    rrr_con_unbal_oos5v=matrix(NA,nw,10)
    
    
    
        
    rrr_con_unbal_is1v=matrix(NA,nw,10)
    rrr_con_unbal_is2v=matrix(NA,nw,10)
    rrr_con_unbal_is3v=matrix(NA,nw,10)
    rrr_con_unbal_is4v=matrix(NA,nw,10)
    rrr_con_unbal_is5v=matrix(NA,nw,10)
    
    
    rrr_unc_is1v=matrix(NA,nw,10)
    rrr_unc_is2v=matrix(NA,nw,10)
    rrr_unc_is3v=matrix(NA,nw,10)
    rrr_unc_is4v=matrix(NA,nw,10)
    rrr_unc_is5v=matrix(NA,nw,10)
    
    
    rrr_bal_iNv=matrix(NA,nw,1)
    rrr_unb_iNv=matrix(NA,nw,1)
    
    
    rrr_unc_is1_vec=array(NA,c(window,10,nw))
    rrr_unc_is2_vec=array(NA,c(window,10,nw))
    rrr_unc_is3_vec=array(NA,c(window,10,nw))
    rrr_unc_is4_vec=array(NA,c(window,10,nw))
    rrr_unc_is5_vec=array(NA,c(window,10,nw))
    
    rrr_con_is1_vec=array(NA,c(window,10,nw))
    rrr_con_is2_vec=array(NA,c(window,10,nw))
    rrr_con_is3_vec=array(NA,c(window,10,nw))
    rrr_con_is4_vec=array(NA,c(window,10,nw))
    rrr_con_is5_vec=array(NA,c(window,10,nw))
    
    rrr_con_unbal_is1_vec=array(NA,c(window,10,nw))
    rrr_con_unbal_is2_vec=array(NA,c(window,10,nw))
    rrr_con_unbal_is3_vec=array(NA,c(window,10,nw))
    rrr_con_unbal_is4_vec=array(NA,c(window,10,nw))
    rrr_con_unbal_is5_vec=array(NA,c(window,10,nw))

    

    
      
          for (i in 1:(nw - 2-window))
    {
      rrr_con_oosv[i]= supp_new[[i]][[1]]
      rrr_con_oos22v[i]= supp_new[[i]][[2]]
      rrr_conv_oosv[i]= supp_new[[i]][[3]]
      rrr_conv_oos2v[i]= supp_new[[i]][[4]]
      rrr_con_unbal_oosv[i]= supp_new[[i]][[5]]
      rrr_con_unbal_oos2v[i]= supp_new[[i]][[6]]
      rrr_conv_unbal_oosv[i]= supp_new[[i]][[7]]
      rrr_conv_unbal_oos2v[i]= supp_new[[i]][[8]]
      rrr_con_unbal2_oosv[i]= supp_new[[i]][[9]]
      rrr_con_unbal2_oos2v[i]= supp_new[[i]][[10]]
      rrr_conv_unbal2_oosv[i]= supp_new[[i]][[11]]
      rrr_conv_unbal2_oos2v[i]= supp_new[[i]][[12]]

      rrr_unc_oos1v[i,]=supp_new[[i]][[13]]
      rrr_unc_oos2v[i,]=supp_new[[i]][[14]]
      rrr_unc_oos3v[i,]=supp_new[[i]][[15]]
      rrr_unc_oos4v[i,]=supp_new[[i]][[16]]
      rrr_unc_oos5v[i,]=supp_new[[i]][[17]]
      
      rrr_con_oos1v[i,]=supp_new[[i]][[18]]
      rrr_con_oos2v[i,]=supp_new[[i]][[19]]
      rrr_con_oos3v[i,]=supp_new[[i]][[20]]
      rrr_con_oos4v[i,]=supp_new[[i]][[21]]
      rrr_con_oos5v[i,]=supp_new[[i]][[22]]
      
      rrr_con_oos3vdue[i]=supp_new[[i]][[23]]
      
      
      rrr_con_is1v[i,]=supp_new[[i]][[24]]
      rrr_con_is2v[i,]=supp_new[[i]][[25]]
      rrr_con_is3v[i,]=supp_new[[i]][[26]]
      rrr_con_is4v[i,]=supp_new[[i]][[27]]
      rrr_con_is5v[i,]=supp_new[[i]][[28]]
  
      
      
      rrr_con_unbal_oos1v[i,]=supp_new[[i]][[29]]
      rrr_con_unbal_oos2v[i,]=supp_new[[i]][[30]]
      rrr_con_unbal_oos3v[i,]=supp_new[[i]][[31]]
      rrr_con_unbal_oos4v[i,]=supp_new[[i]][[32]]
      rrr_con_unbal_oos5v[i,]=supp_new[[i]][[33]]
      
  
      rrr_con_unbal_is1v[i,]=supp_new[[i]][[34]]
      rrr_con_unbal_is2v[i,]=supp_new[[i]][[35]]
      rrr_con_unbal_is3v[i,]=supp_new[[i]][[36]]
      rrr_con_unbal_is4v[i,]=supp_new[[i]][[37]]
      rrr_con_unbal_is5v[i,]=supp_new[[i]][[38]]
      
      
      rrr_unc_is1v[i,]=supp_new[[i]][[39]]
      rrr_unc_is2v[i,]=supp_new[[i]][[40]]
      rrr_unc_is3v[i,]=supp_new[[i]][[41]]
      rrr_unc_is4v[i,]=supp_new[[i]][[42]]
      rrr_unc_is5v[i,]=supp_new[[i]][[43]]

      
      rrr_bal_iNv[i,]=supp_new[[i]][[44]]
      rrr_unb_iNv[i,]=supp_new[[i]][[45]]
      
      
      
      
      rrr_unc_is1_vec[,,i]=supp_new[[i]][[46]]
      rrr_unc_is2_vec[,,i]=supp_new[[i]][[47]]
      rrr_unc_is3_vec[,,i]=supp_new[[i]][[48]]
      rrr_unc_is4_vec[,,i]=supp_new[[i]][[49]]
      rrr_unc_is5_vec[,,i]=supp_new[[i]][[50]]
      
      rrr_con_is1_vec[,,i]=supp_new[[i]][[51]]
      rrr_con_is2_vec[,,i]=supp_new[[i]][[52]]
      rrr_con_is3_vec[,,i]=supp_new[[i]][[53]]
      rrr_con_is4_vec[,,i]=supp_new[[i]][[54]]
      rrr_con_is5_vec[,,i]=supp_new[[i]][[55]]
      
      rrr_con_unbal_is1_vec[,,i]=supp_new[[i]][[56]]
      rrr_con_unbal_is2_vec[,,i]=supp_new[[i]][[57]]
      rrr_con_unbal_is3_vec[,,i]=supp_new[[i]][[58]]
      rrr_con_unbal_is4_vec[,,i]=supp_new[[i]][[59]]
      rrr_con_unbal_is5_vec[,,i]=supp_new[[i]][[60]]
      
   
      
    }
    


     rrr=matrix(NA,nw,1)
     SR=matrix(NA,nw,1)
     NAV=matrix(1,nw,1)
     rrrb=matrix(NA,nw,1)
     
     t=Thalf-window+2
     for (t in (Thalf-window+2):(nw-window-1))
     {
       rrr[t]=rrr_con_unbal_oos1v[t,ts.nfactorhat[t-window-1]]
       rrrb[t]=rrr_con_oos1v[t,ts.nfactorhat[t-window-1]]

          }

     plot(rrr[(Thalf-window-1):(nw-window-1)])
     plot(rrrb[(Thalf-window-1):(nw-window-1)])

     
     
       
       NAV=matrix(1,nw,1)
       NAV2=matrix(1,nw,1)
       NAV3=matrix(1,nw,1)
       
       rrr=matrix(NA,nw,1)
       rrr2=matrix(NA,nw,1)
       rrr3=matrix(NA,nw,1)
       
       SRrrr=matrix(NA,nw,1)
       SRrrr2=matrix(NA,nw,1)
       SRrrr3=matrix(NA,nw,1)
       
       
       SRrrris=matrix(NA,nw,1)
       SRrrris2=matrix(NA,nw,1)
       SRrrris3=matrix(NA,nw,1)
      
       SRrrrisvec=matrix(NA,nw,10)
       SRrrris2vec=matrix(NA,nw,10)
       SRrrris3vec=matrix(NA,nw,10)
       
       
       
       SR=matrix(NA,nw,10)
       SR2=matrix(NA,nw,10)
       SR3=matrix(NA,nw,10)
       
       
       
       SRmv=matrix(NA,nw,10)
       SR2mv=matrix(NA,nw,10)
       SR3mv=matrix(NA,nw,10)
       

       SR=matrix(NA,nw,10)
       SR2=matrix(NA,nw,10)
       SR3=matrix(NA,nw,10)
       SR3uoos=matrix(NA,nw,10)
       SR3uoos2=matrix(NA,nw,10)
       SR3uoosv=matrix(NA,nw,10)
       SR3uoosv2=matrix(NA,nw,10)
       
       
       
       NAVisvec=matrix(1,nw,10)
       NAVis2vec=matrix(1,nw,10)
       NAVis3vec=matrix(1,nw,10)
       
       NAVis=matrix(1,nw,1)
       NAVis2=matrix(1,nw,1)
       NAVis3=matrix(1,nw,1)
       
       NAVvec=matrix(1,nw,10)
       NAV2vec=matrix(1,nw,10)
       NAV3vec=matrix(1,nw,10)
       

       NAV1N=matrix(1,nw,1)
       NAV1Nbal=matrix(1,nw,1)
       
       
    
       
       
       t=1
       t=(Thalf-window+2)
       for (t in (Thalf-window+2):(nw-window-2))
       
       {
         rrr[t]=rrr_con_unbal_oos1v[t,ts.nfactorhat[t]]
         rrr2[t]=rrr_con_oos1v[t,ts.nfactorhat[t]]
         rrr3[t]=rrr_unc_oos1v[t,ts.nfactorhat[t]]
         
    
         SRmv[t,]=sqrt(12)*apply(rrr_con_unbal_oos2v[(t-window+1):t,],2,mean)/apply(rrr_con_unbal_oos2v[(t-window+1):t,],2,sd)
         SR2mv[t,]=sqrt(12)*apply(rrr_con_oos2v[(t-window+1):t,],2,mean)/apply(rrr_con_oos2v[(t-window+1):t,],2,sd)
         SR3mv[t,]=sqrt(12)*apply(rrr_unc_oos2v[(t-window+1):t,],2,mean)/apply(rrr_unc_oos2v[(t-window+1):t,],2,sd)
         
         
         
         SR[t,]=sqrt(12)*apply(rrr_con_unbal_oos1v[(t-window+1):t,],2,mean)/apply(rrr_con_unbal_oos1v[(t-window+1):t,],2,sd)
         SR2[t,]=sqrt(12)*apply(rrr_con_oos1v[(t-window+1):t,],2,mean)/apply(rrr_con_oos1v[(t-window+1):t,],2,sd)
         SR3[t,]=sqrt(12)*apply(rrr_unc_oos1v[(t-window+1):t,],2,mean)/apply(rrr_unc_oos1v[(t-window+1):t,],2,sd)
 
                 if (t > (Thalf+window))
{           SR3uoos[t,]=sqrt(12)*apply(rrr_unc_oos[(t-window+1-Thalf):(t-Thalf),],2,mean)/apply(rrr_unc_oos[(t-window+1-Thalf):(t-Thalf),],2,sd)
           SR3uoos2[t,]=sqrt(12)*apply(rrr_unc_oos2[(t-window+1-Thalf):(t-Thalf),],2,mean)/apply(rrr_unc_oos2[(t-window+1-Thalf):(t-Thalf),],2,sd)
           SR3uoosv[t,]=sqrt(12)*apply(rrr_uncv_oos[(t-window+1-Thalf):(t-Thalf),],2,mean)/apply(rrr_uncv_oos[(t-window+1-Thalf):(t-Thalf),],2,sd)
          SR3uoosv2[t,]=sqrt(12)*apply(rrr_uncv_oos2[(t-window+1-Thalf):(t-Thalf),],2,mean)/apply(rrr_uncv_oos2[(t-window+1-Thalf):(t-Thalf),],2,sd)
         }        
         
           
            
          
     SRrrr[t]=sqrt(12)*mean(rrr[(t-window+1):t,1])/sd(rrr[(t-window+1):t,1])
     SRrrr2[t]=sqrt(12)*mean(rrr2[(t-window+1):t,1])/sd(rrr2[(t-window+1):t,1])
     SRrrr3[t]=sqrt(12)*mean(rrr3[(t-window+1):t,1])/sd(rrr3[(t-window+1):t,1])
   
     SRrrris3[t]=sqrt(12)*mean(rrr_unc_is1_vec[,ts.nfactorhat[t],t])/sd(rrr_unc_is1_vec[,ts.nfactorhat[t],t])
     SRrrris[t]=sqrt(12)*mean(rrr_con_unbal_is1_vec[,ts.nfactorhat[t],t])/sd(rrr_con_unbal_is1_vec[,ts.nfactorhat[t],t])
     SRrrris2[t]=sqrt(12)*mean(rrr_con_is1_vec[,ts.nfactorhat[t],t])/sd(rrr_con_is1_vec[,ts.nfactorhat[t],t])
     
    
    SRrrris3vec[t,]=sqrt(12)*apply(rrr_unc_is1_vec[,,t],2,mean)/apply(rrr_unc_is1_vec[,,t],2,sd)
    SRrrrisvec[t,]=sqrt(12)*apply(rrr_con_is1_vec[,,t],2,mean)/apply(rrr_con_is1_vec[,,t],2,sd)
    SRrrris2vec[t,]=sqrt(12)*apply(rrr_con_unbal_is1_vec[,,t],2,mean)/apply(rrr_con_unbal_is1_vec[,,t],2,sd)
    
    NAV1N[t]=NAV1N[t-1]*exp(rrr_bal_iNv[t])
    NAV1Nbal[t]=NAV1Nbal[t-1]*exp( rrr_unb_iNv[t])
    
     
       
              NAVis[t]=NAVis[t-1]*exp(rrr_con_unbal_is1_vec[window,ts.nfactorhat[t],t])
              NAVis2[t]=NAVis2[t-1]*exp(rrr_con_is1_vec[window,ts.nfactorhat[t],t])
              NAVis3[t]=NAVis3[t-1]*exp(rrr_unc_is1_vec[window,ts.nfactorhat[t],t])
              
       
              NAV[t]=NAV[t-1]*exp(rrr[t])
              NAV2[t]=NAV2[t-1]*exp(rrr2[t])
              NAV3[t]=NAV3[t-1]*exp(rrr3[t])
    
              
              SR[t,]=sqrt(12)*apply(rrr_con_unbal_oos1v[(t-window+1):t,],2,mean)/apply(rrr_con_unbal_oos1v[(t-window+1):t,],2,sd)
              SR2[t,]=sqrt(12)*apply(rrr_con_oos1v[(t-window+1):t,],2,mean)/apply(rrr_con_oos1v[(t-window+1):t,],2,sd)
              SR3[t,]=sqrt(12)*apply(rrr_unc_oos1v[(t-window+1):t,],2,mean)/apply(rrr_unc_oos1v[(t-window+1):t,],2,sd)
              
              
              
              
              
              
              
              
              
              NAVvec[t,]=NAVvec[t-1,]*exp(rrr_con_unbal_oos1v[t,])
              NAV2vec[t,]=NAV2vec[t-1,]*exp(rrr_con_oos1v[t,])
              NAV3vec[t,]=NAV3vec[t-1,]*exp(rrr_unc_oos1v[t,])
              
              
                        
              
              NAVisvec[t,]=NAVisvec[t-1,]*exp(rrr_con_unbal_is1_vec[window,,t])
              NAVis2vec[t,]=NAVis2vec[t-1,]*exp(rrr_con_is1_vec[window,,t])
              NAVis3vec[t,]=NAVis3vec[t-1,]*exp(rrr_unc_is1_vec[window,,t])
              
         
       }
          
       # bunch of plots and averages - used for table
       {
         
         

         
         
         
         
       plot(NAV[Thalf:(nw-window-2)])
       points(NAV2[Thalf:(nw-window-2)],col=2)
       points(NAV3[Thalf:(nw-window-2)],col=3)
       
    
       plot(NAVvec[Thalf:(nw-window-2),1])
       points(NAV2vec[Thalf:(nw-window-2),1],col=2)
       points(NAV3vec[Thalf:(nw-window-2),1],col=3)
       
   
       plot(NAVvec[Thalf:(nw-window-2),5])
       points(NAV2vec[Thalf:(nw-window-2),5],col=2)
       points(NAV3vec[Thalf:(nw-window-2),5],col=3)
       
       
           
       points(NAV3[Thalf:(nw-window-2)],col=3)
       cbind(NAV,NAV2,NAV3,(1:nw))
       cbind(rrr,rrr2,rrr3,(1:nw))
       cbind(rrr_con_unbal_oos1v[,1],rrr_con_oos1v[,1],rrr_unc_oos1v[,1],(1:nw))
       plot(rrr_con_unbal_oos1v[,1])
       points(rrr_con_oos1v[,1],col=2)
       points(rrr_unc_oos1v[,1],col=3)
     
       
       plot(NAV2vec[Thalf:(nw-window-2),1])
       points(NAV3vec[Thalf:(nw-window-2),1],col=2)
       points(NAVvec[Thalf:(nw-window-2),1],col=3)
       
         
       
       
       plot(rrr[Thalf:(nw-window-2)])
       points(rrr2[Thalf:(nw-window-2)],col=2)
       points(rrr3[Thalf:(nw-window-2)],col=3)
   
       
       plot(rrr[Thalf:(nw-window-2)])
       plot(rrr2[Thalf:(nw-window-2)])
       plot(rrr3[Thalf:(nw-window-2)])
       
           
       plot(SRrrr[(Thalf+window):(nw-window-2)],ylim=c(-3,3))
       points(SRrrr2[(Thalf+window):(nw-window-2)],col=2)
       points(SRrrr3[(Thalf+window):(nw-window-2)],col=3)
       
       lines(rrr[(Thalf+window):(nw-window-2)],col=2)
       plot(SRrrr2[(Thalf+window):(nw-window-2)])
       lines(rrr2[(Thalf+window):(nw-window-2)],col=2)
       plot(SRrrr3[(Thalf+window):(nw-window-2)])
       lines(rrr3[(Thalf+window):(nw-window-2)],col=2)
       
       # choose mean
       mean(SRrrr[(Thalf+window):(nw-window-2)])
       mean(SRrrr2[(Thalf+window):(nw-window-2)])
       mean(SRrrr3[(Thalf+window):(nw-window-2)])# excellent we beat it!
     
       median(SRrrr[(Thalf+window):(nw-window-2)])
       median(SRrrr2[(Thalf+window):(nw-window-2)])
       median(SRrrr3[(Thalf+window):(nw-window-2)])# excellent we beat it!
       
       
         
       hist(SRrrr2[(Thalf+window):(nw-window-2)],plot=TRUE,col=2,xlim=c(-3,3),ylim=c(0,50),breaks=30)
       hist(SRrrr3[(Thalf+window):(nw-window-2)],add=TRUE,col=1,breaks=30)
       hist(SRrrr[(Thalf+window):(nw-window-2)],add=TRUE,col=3,breaks=30)
      
       hist(SR2[(Thalf+window):(nw-window-2),1],add=TRUE,col=2,xlim=c(-3,3),ylim=c(0,40),breaks=30)
       hist(SR3[(Thalf+window):(nw-window-2),1],add=TRUE,col=1,breaks=30)
       hist(SR[(Thalf+window):(nw-window-2),1],add=TRUE,col=3,breaks=30)
     #  hist(SR_uncv_oos[(Thalf+window):(nw-window-2),1],add=TRUE,col=1,breaks=30)
       
        
       mean(SRrrris[(Thalf+window):(nw-window-2)])
       mean(SRrrris2[(Thalf+window):(nw-window-2)])
       mean(SRrrris3[(Thalf+window):(nw-window-2)])# excellent we beat it!
   
       hist(SRrrris2[(Thalf+window):(nw-window-2)],plot=TRUE,col=2,xlim=c(-3,3),ylim=c(0,50))
       hist(SRrrris3[(Thalf+window):(nw-window-2)],add=TRUE,col=1)
       hist(SRrrris[(Thalf+window):(nw-window-2)],add=TRUE,col=3)
       

       apply(SRrrrisvec[(Thalf+window):(nw-window-2),],2,mean)
       apply(SRrrris2vec[(Thalf+window):(nw-window-2),],2,mean)
       apply(SRrrris3vec[(Thalf+window):(nw-window-2),],2,mean)# excellent we beat it!
       
              

       # comment: here i did not get rid of the crazy returns. do it.
       # comment: these not to be used for unco, use the other ones 8 oct line 9170 (now 9073-9076)
       # use SRrrr and SRrrr2 for con and con_unbal.  SRrrr3 is too harsh.
       apply(SR[(Thalf+window):(nw-window-2),],2,mean)
       plot(SR[(Thalf+window):(nw-window-2),1])
       apply(SR2[(Thalf+window):(nw-window-2),],2,mean)
       points(SR2[(Thalf+window):(nw-window-2),1],col=2)
       apply(SR3[(Thalf+window):(nw-window-2),],2,mean)
       points(SR3[(Thalf+window):(nw-window-2),1],col=3)
       }
       
       
       #PLOTS  
       

# these are the single SR for unc (not rolling)  - draw vertical line
      SR_unc_oos1v= sqrt(12)*apply(rrr_unc_oos1v[240:431,], 2, mean)/apply(rrr_unc_oos1v[240:431,], 2, sd)
      SR_unc_oos2v=  sqrt(12)*apply(rrr_unc_oos2v[240:431,], 2, mean)/apply(rrr_unc_oos2v[240:431,], 2, sd)
       sqrt(12)*apply(rrr_unc_oos3v[240:431,], 2, mean)/apply(rrr_unc_oos3v[240:431,], 2, sd)
       sqrt(12)*apply(rrr_unc_oos4v[240:431,], 2, mean)/apply(rrr_unc_oos4v[240:431,], 2, sd)
       sqrt(12)*apply(rrr_unc_oos5v[240:431,], 2, mean)/apply(rrr_unc_oos5v[240:431,], 2, sd)
       



       
       
       # construct new colors - alpha gives the strength of the color
       c1 <- rgb(173,216,230, max = 255, alpha = 200, names = "lt.blue")
       c2 <- rgb(255,192,203, max = 255, alpha = 30, names = "lt.pink")
       c3 <- rgb(144,238,144, max = 255, alpha = 100, names = "lt.green")
       c4 <- rgb(255,0,0, max = 255, alpha = 80, names = "lt.red")
       
         

       
        hSR <- hist(SR[(Thalf-window+2):(nw-window-2),1], breaks = 30 , plot = FALSE)
        hSR2 <- hist(SR2[(Thalf-window+2):(nw-window-2),1], breaks = 30 , plot = FALSE)
        hSR3 <- hist(SR3[(Thalf-window+2):(nw-window-2),1] , breaks = 30 , plot = FALSE)
       
       
       
       #  
       pdf('T120_SR_K1_9Jan23.pdf')
       plot(hSR,  main="
            Portfolio Performance: Sharpe Ratio - Tangency Portolios
\n   Out-of-Sample: local PCA  vs. constant PCA  (T=120) \n" ,xlim=c(-0.5,1),ylim=c(0,25),
            xlab="Out-of-Sample Sharpe Ratio (p.a.)",col = c1) # Plot 1st histogram using a transparent color
       plot(hSR2,  col = c3, add = TRUE) # Add 2nd histogram using different color
       abline(v=0.579,col=2,lwd=5)
       legend(-0.5,20,c("local PCA unb","local PCA bal", "constant PCA"), lwd=c(5,5), col=c(c1,c3,2), y.intersp=0.75)
              dev.off()
    

       hSR <- hist(SRrrrisvec[(Thalf-window+2):(nw-window-2),1], breaks = 30 , plot = FALSE)
       hSR2 <- hist(SRrrris2vec[(Thalf-window+2):(nw-window-2),1], breaks = 30 , plot = FALSE)
       hSR3 <- hist(SRrrris3vec[(Thalf-window+2):(nw-window-2),1] , breaks = 30 , plot = FALSE)
       


       
       pdf('T120_SR_K1_is_9Jan23.pdf')

       plot(hSR,  main="
            Portfolio Performance: Sharpe Ratio - Tangency Portolios
\n In-Sample: local PCA  vs. constant PCA  (T=120) \n" ,xlim=c(-0.5,1.5),ylim=c(0,25),
            xlab="In-Sample Sharpe Ratio (p.a.)",col = c1) # Plot 1st histogram using a transparent color
       plot(hSR2,  col = c3, add = TRUE) # Add 2nd histogram using different color
       abline(v=0.459,col=2,lwd=5)
       legend(-0.5,20,c("local PCA unb","local PCA bal", "constant PCA"), lwd=c(5,5), col=c(c1,c3,2), y.intersp=0.75)
                     dev.off()
       
       
       plot(SR[(Thalf+window):(nw-window-2),1],add=TRUE,col=1,type="l",lwd=5)
       lines(SR2[(Thalf+window):(nw-window-2),1],add=TRUE,col=2,lwd=5)
       abline(h=0.579,col=3,lwd=5)
       
       
       
       
       
      hist(SR[(Thalf+window):(nw-window-2),1],plot=TRUE,col=1,xlim=c(-3,3),ylim=c(0,50))
       hist(SR2[(Thalf+window):(nw-window-2),1],add=TRUE,col=2)
       hist(SR3[(Thalf+window):(nw-window-2),1],add=TRUE,col=3)
       
       plot(rrr_con_unbal_oos1v[(Thalf+window):(nw-window-2),1])
       points(rrr_con_oos1v[(Thalf+window):(nw-window-2),1],col=2)
       points(rrr_unc_oos1v[(Thalf+window):(nw-window-2),1],col=3)
       
     
       plot(NAVvec[(Thalf+window):(nw-window-2),1],ylim=c(0,10))
       points(NAV2vec[(Thalf+window):(nw-window-2),1],col=2)
       points(NAV3vec[(Thalf+window):(nw-window-2),1],col=3)
       points(NAV1N[(Thalf+window):(nw-window-2),1],col=4)
       points(NAV1Nbal[(Thalf+window):(nw-window-2),1],col=5)
       
              
       
       plot(NAVisvec[(Thalf+window):(nw-window-2),1])
       points(NAVis2vec[(Thalf+window):(nw-window-2),1],col=2)
       points(NAVis3vec[(Thalf+window):(nw-window-2),1],col=3)
       
     

       
       
       
       # k=3 
       
       plot(SR[(Thalf+window):(nw-window-2),3],add=TRUE,col=1,type="l",lwd=5)
       lines(SR2[(Thalf+window):(nw-window-2),3],add=TRUE,col=2,lwd=5)
       abline(h=0.127,col=3,lwd=5)
       
       
          
       hSR <- hist(SR[(Thalf-window+2):(nw-window-2),3], breaks = 30 , plot = FALSE)
       hSR2 <- hist(SR22[(Thalf-window+2):(nw-window-2),3], breaks = 30 , plot = FALSE)
       hSR3 <- hist(SR3[(Thalf-window+2):(nw-window-2),3] , breaks = 30 , plot = FALSE)
       
            
       pdf('T120_SR_K3_9Jan23.pdf')
       plot(hSR,  main="
            Portfolio Performance: Sharpe Ratio - Tangency Portolios
\n Out-of-Sample: local PCA  vs. constant PCA  (T=120) \n" ,xlim=c(-0.5,1.5),ylim=c(0,25),
            xlab="Out-of-Sample Sharpe Ratio (p.a.)",col = c1) # Plot 1st histogram using a transparent color
       plot(hSR2,  col = c3, add = TRUE) # Add 2nd histogram using different color
       abline(v=0.127,col=2,lwd=5)
       legend(-0.5,20,c("local PCA unb","local PCA bal", "constant PCA"), lwd=c(5,5), col=c(c1,c3,2), y.intersp=0.75)
       
       
       dev.off()
       
       
       
       hSR <- hist(SRrrrisvec[(Thalf-window+2):(nw-window-2),3], breaks = 30 , plot = FALSE)
       hSR2 <- hist(SRrrris2vec[(Thalf-window+2):(nw-window-2),3], breaks = 30 , plot = FALSE)
       hSR3 <- hist(SRrrris3vec[(Thalf-window+2):(nw-window-2),3] , breaks = 30 , plot = FALSE)
       

       
       
       pdf('T120_SR_K3_is_9Jan23.pdf')
       plot(hSR,  main="
            Portfolio Performance: Sharpe Ratio - Tangency Portolios
\n In-Sample: local PCA  vs. constant PCA  (T=120) \n" ,xlim=c(-0.5,1.5),ylim=c(0,30),
            xlab="In-Sample Sharpe Ratio (p.a.)",col = c1) # Plot 1st histogram using a transparent color
       plot(hSR2,  col = c3, add = TRUE) # Add 2nd histogram using different color
       abline(v=0.522,col=2,lwd=5)
       legend(-2,20,c("local PCA unb","local PCA bal", "constant PCA"), lwd=c(5,5), col=c(c1,c3,2), y.intersp=0.75)
       
       
       
                     dev.off()
       
       
       
       
       

       # k=7   
       
    
       hSR <- hist(SR[(Thalf-window+2):(nw-window-2),7], breaks = 30 , plot = FALSE)
       hSR2 <- hist(SR2[(Thalf-window+2):(nw-window-2),7], breaks = 30 , plot = FALSE)
       hSR3 <- hist(SR3[(Thalf-window+2):(nw-window-2),7] , breaks = 30 , plot = FALSE)
       
       
       pdf('T120_SR_K7_9Jan23.pdf')
       plot(hSR,  main="
            Portfolio Performance: Sharpe Ratio - Tangency Portolios
\n  Out-of-Sample: local PCA  vs. constant PCA  (T=120) \n" ,xlim=c(-0.5,1.5),ylim=c(0,25),
            xlab="Out-of-Sample: Sharpe Ratio (p.a.)",col = c1) # Plot 1st histogram using a transparent color
       plot(hSR2,  col = c3, add = TRUE) # Add 2nd histogram using different color
       abline(v= -0.294,col=2,lwd=5)
       
       legend(-0.5,20,c("local PCA unb","local PCA bal", "constant PCA"), lwd=c(5,5), col=c(c1,c3,2), y.intersp=0.75)
       dev.off()
       
   
      

       hSR <- hist(SRrrrisvec[(Thalf-window+2):(nw-window-2),7], breaks = 30 , plot = FALSE)
       hSR2 <- hist(SRrrris2vec[(Thalf-window+2):(nw-window-2),7], breaks = 30 , plot = FALSE)
       hSR3 <- hist(SRrrris3vec[(Thalf-window+2):(nw-window-2),7] , breaks = 30 , plot = FALSE)
       

       
           
       pdf('T120_SR_K7_is_9Jan23.pdf')
       plot(hSR,  main="
            Portfolio Performance: Sharpe Ratio - Tangency Portolios
\n In-Sample: local PCA  vs. constant PCA  (T=120) \n" ,xlim=c(-0.5,2),ylim=c(0,30),
            xlab="In-Sample Sharpe Ratio (p.a.)",col = c1) # Plot 1st histogram using a transparent color
       plot(hSR2,  col = c3, add = TRUE) # Add 2nd histogram using different color
       abline(v= 0.674,col=2,lwd=5)
       
       legend(-0.5,20,c("local PCA unb","local PCA bal", "constant PCA"), lwd=c(5,5), col=c(c1,c3,2), y.intersp=0.75)
       
       
              dev.off()
       
       

       
              
       #k=10
       

       hSR <- hist(SR[(Thalf-window+2):(nw-window-2),10], breaks = 30 , plot = FALSE)
       hSR2 <- hist(SR2[(Thalf-window+2):(nw-window-2),10], breaks = 30 , plot = FALSE)
       hSR3 <- hist(SR3[(Thalf-window+2):(nw-window-2),10] , breaks = 30 , plot = FALSE)
       
       
            
       pdf('T120_SR_K10_9Jan23.pdf')
       plot(hSR,  main="
            Portfolio Performance: Sharpe Ratio - Tangency Portolios
\n Out-of-Sample: local PCA  vs. constant PCA  (T=120) \n" ,xlim=c(-0.5,1.5),ylim=c(0,25),
            xlab="Out-of-Sample Sharpe Ratio (p.a.)",col = c1) # Plot 1st histogram using a transparent color
       plot(hSR2,  col = c3, add = TRUE) # Add 2nd histogram using different color
       abline(v=  -0.279,col=2,lwd=5)
       legend(-0.5,20,c("local PCA unb","local PCA bal", "constant PCA"), lwd=c(5,5), col=c(c1,c3,2), y.intersp=0.75)
       
       
       
             dev.off()
       
       

       hSR <- hist(SRrrrisvec[(Thalf-window+2):(nw-window-2),10], breaks = 30 , plot = FALSE)
       hSR2 <- hist(SRrrris2vec[(Thalf-window+2):(nw-window-2),10], breaks = 30 , plot = FALSE)
       hSR3 <- hist(SRrrris3vec[(Thalf-window+2):(nw-window-2),10] , breaks = 30 , plot = FALSE)
       

       
       
       pdf('T120_SR_K10_is_9Jan23.pdf')
       plot(hSR,  main="
            Portfolio Performance: Sharpe Ratio - Tangency Portolios
\n In-Sample: local PCA  vs. constant PCA  (T=120) \n" ,xlim=c(-0.5,2),ylim=c(0,30),
            xlab="In-Sample Sharpe Ratio (p.a.)",col = c1) # Plot 1st histogram using a transparent color
       plot(hSR2,  col = c3, add = TRUE) # Add 2nd histogram using different color
       abline(v= 1.014,col=2,lwd=5)
       legend(-0.5,20,c("local PCA unb","local PCA bal", "constant PCA"), lwd=c(5,5), col=c(c1,c3,2), y.intersp=0.75)
       
       
              dev.off()
       
       

       
       # END OF PLOTS
       

        # case T=120 
        
        rrr=matrix(NA,nw,1)
        SR=matrix(NA,nw,1)
        NAV=matrix(1,nw,1)
        rrrb=matrix(NA,nw,1)
        

        

        t=Thalf-window+2
        for (t in (Thalf-window+2):(nw-window-1))
        {
          rrr[t]=rrr_con_unbal_oos1v[t,ts.nfactorhat[t-window-1]]
          rrrb[t]=rrr_con_oos1v[t,ts.nfactorhat[t-window-1]]
          
        }
        
        plot(rrr[(Thalf-window-1):(nw-window-1)])
        points(rrrb[(Thalf-window-1):(nw-window-1)],col=2)
        
        
        
        
        NAV=matrix(1,nw,1)
        NAV2=matrix(1,nw,1)
        NAV3=matrix(1,nw,1)
        
        rrr=matrix(NA,nw,1)
        rrr2=matrix(NA,nw,1)
        rrr3=matrix(NA,nw,1)
        
        SRrrr=matrix(NA,nw,1)
        SRrrr2=matrix(NA,nw,1)
        SRrrr3=matrix(NA,nw,1)
        
        
        SRrrris=matrix(NA,nw,1)
        SRrrris2=matrix(NA,nw,1)
        SRrrris3=matrix(NA,nw,1)
        
        SRrrrisvec=matrix(NA,nw,10)
        SRrrris2vec=matrix(NA,nw,10)
        SRrrris3vec=matrix(NA,nw,10)
        
        
        
        SR=matrix(NA,nw,10)
        SR2=matrix(NA,nw,10)
        SR3=matrix(NA,nw,10)
        
        
        
        SRmv=matrix(NA,nw,10)
        SR2mv=matrix(NA,nw,10)
        SR3mv=matrix(NA,nw,10)
        
        
        SR=matrix(NA,nw,10)
        SR2=matrix(NA,nw,10)
        SR3=matrix(NA,nw,10)
        SR3uoos=matrix(NA,nw,10)
        SR3uoos2=matrix(NA,nw,10)
        SR3uoosv=matrix(NA,nw,10)
        SR3uoosv2=matrix(NA,nw,10)
        
        
        
        NAVisvec=matrix(1,nw,10)
        NAVis2vec=matrix(1,nw,10)
        NAVis3vec=matrix(1,nw,10)
        
        NAVis=matrix(1,nw,1)
        NAVis2=matrix(1,nw,1)
        NAVis3=matrix(1,nw,1)
        
        NAVvec=matrix(1,nw,10)
        NAV2vec=matrix(1,nw,10)
        NAV3vec=matrix(1,nw,10)
        
        
        NAV1N=matrix(1,nw,1)
        NAV1Nbal=matrix(1,nw,1)
        
        
       
        
    
  t=2
  #            for (t in (Thalf-window+2):(nw-window-2))
              for (t in 2:(nw-window-2))
  
        {
          rrr[t]=rrr_con_unbal_oos1v[t,ts.nfactorhat[t]]
          rrr2[t]=rrr_con_oos1v[t,ts.nfactorhat[t]]
          rrr3[t]=rrr_unc_oos1v[t,ts.nfactorhat[t]]
        }
        
        t=(Thalf-window+2)
    
          for (t in (Thalf-window+2):(nw-window-2))
          
        {
          
          SRmv[t,]=sqrt(12)*apply(rrr_con_unbal_oos2v[(t-window+1):t,],2,mean)/apply(rrr_con_unbal_oos2v[(t-window+1):t,],2,sd)
          SR2mv[t,]=sqrt(12)*apply(rrr_con_oos2v[(t-window+1):t,],2,mean)/apply(rrr_con_oos2v[(t-window+1):t,],2,sd)
          SR3mv[t,]=sqrt(12)*apply(rrr_unc_oos2v[(t-window+1):t,],2,mean)/apply(rrr_unc_oos2v[(t-window+1):t,],2,sd)
          
          
          
          SR[t,]=sqrt(12)*apply(rrr_con_unbal_oos1v[(t-window+1):t,],2,mean)/apply(rrr_con_unbal_oos1v[(t-window+1):t,],2,sd)
          SR2[t,]=sqrt(12)*apply(rrr_con_oos1v[(t-window+1):t,],2,mean)/apply(rrr_con_oos1v[(t-window+1):t,],2,sd)
          SR3[t,]=sqrt(12)*apply(rrr_unc_oos1v[(t-window+1):t,],2,mean)/apply(rrr_unc_oos1v[(t-window+1):t,],2,sd)
          
          
            SR3uoos[t,]=sqrt(12)*apply(rrr_unc_oos[(t-window+1-Thalf):(t-Thalf),],2,mean)/apply(rrr_unc_oos[(t-window+1-Thalf):(t-Thalf),],2,sd)
            SR3uoos2[t,]=sqrt(12)*apply(rrr_unc_oos2[(t-window+1-Thalf):(t-Thalf),],2,mean)/apply(rrr_unc_oos2[(t-window+1-Thalf):(t-Thalf),],2,sd)
            SR3uoosv[t,]=sqrt(12)*apply(rrr_uncv_oos[(t-window+1-Thalf):(t-Thalf),],2,mean)/apply(rrr_uncv_oos[(t-window+1-Thalf):(t-Thalf),],2,sd)
            SR3uoosv2[t,]=sqrt(12)*apply(rrr_uncv_oos2[(t-window+1-Thalf):(t-Thalf),],2,mean)/apply(rrr_uncv_oos2[(t-window+1-Thalf):(t-Thalf),],2,sd)
          
                
          
          
          
          SRrrr[t]=sqrt(12)*mean(rrr[(t-window+1):t,1])/sd(rrr[(t-window+1):t,1])
          SRrrr2[t]=sqrt(12)*mean(rrr2[(t-window+1):t,1])/sd(rrr2[(t-window+1):t,1])
          SRrrr3[t]=sqrt(12)*mean(rrr3[(t-window+1):t,1])/sd(rrr3[(t-window+1):t,1])
          
          SRrrris3[t]=sqrt(12)*mean(rrr_unc_is1_vec[,ts.nfactorhat[t],t])/sd(rrr_unc_is1_vec[,ts.nfactorhat[t],t])
          SRrrris[t]=sqrt(12)*mean(rrr_con_unbal_is1_vec[,ts.nfactorhat[t],t])/sd(rrr_con_unbal_is1_vec[,ts.nfactorhat[t],t])
          SRrrris2[t]=sqrt(12)*mean(rrr_con_is1_vec[,ts.nfactorhat[t],t])/sd(rrr_con_is1_vec[,ts.nfactorhat[t],t])
          
          
          SRrrris3vec[t,]=sqrt(12)*apply(rrr_unc_is1_vec[,,t],2,mean)/apply(rrr_unc_is1_vec[,,t],2,sd)
          SRrrrisvec[t,]=sqrt(12)*apply(rrr_con_is1_vec[,,t],2,mean)/apply(rrr_con_is1_vec[,,t],2,sd)
          SRrrris2vec[t,]=sqrt(12)*apply(rrr_con_unbal_is1_vec[,,t],2,mean)/apply(rrr_con_unbal_is1_vec[,,t],2,sd)
          
          NAV1N[t]=NAV1N[t-1]*exp(rrr_bal_iNv[t])
          NAV1Nbal[t]=NAV1Nbal[t-1]*exp( rrr_unb_iNv[t])
          
          
          
          NAVis[t]=NAVis[t-1]*exp(rrr_con_unbal_is1_vec[window,ts.nfactorhat[t],t])
          NAVis2[t]=NAVis2[t-1]*exp(rrr_con_is1_vec[window,ts.nfactorhat[t],t])
          NAVis3[t]=NAVis3[t-1]*exp(rrr_unc_is1_vec[window,ts.nfactorhat[t],t])
          
          
          NAV[t]=NAV[t-1]*exp(rrr[t])
          NAV2[t]=NAV2[t-1]*exp(rrr2[t])
          NAV3[t]=NAV3[t-1]*exp(rrr3[t])
          
          
          SR[t,]=sqrt(12)*apply(rrr_con_unbal_oos1v[(t-window+1):t,],2,mean)/apply(rrr_con_unbal_oos1v[(t-window+1):t,],2,sd)
          SR2[t,]=sqrt(12)*apply(rrr_con_oos1v[(t-window+1):t,],2,mean)/apply(rrr_con_oos1v[(t-window+1):t,],2,sd)
          SR3[t,]=sqrt(12)*apply(rrr_unc_oos1v[(t-window+1):t,],2,mean)/apply(rrr_unc_oos1v[(t-window+1):t,],2,sd)
          
          
          
          
          
          
          
          
          NAVvec[t,]=NAVvec[t-1,]*exp(rrr_con_unbal_oos1v[t,])
          NAV2vec[t,]=NAV2vec[t-1,]*exp(rrr_con_oos1v[t,])
          NAV3vec[t,]=NAV3vec[t-1,]*exp(rrr_unc_oos1v[t,])
          
          
          
          
          NAVisvec[t,]=NAVisvec[t-1,]*exp(rrr_con_unbal_is1_vec[window,,t])
          NAVis2vec[t,]=NAVis2vec[t-1,]*exp(rrr_con_is1_vec[window,,t])
          NAVis3vec[t,]=NAVis3vec[t-1,]*exp(rrr_unc_is1_vec[window,,t])
          
          
        }
          
        # bunch of plots and averages - used for table
        {
          
          
          
          
          
          
          
         
        
        
        
        # k=1      
        plot(SR[(Thalf+window):(nw-window-2),1])
        points(SR2[(Thalf+window):(nw-window-2),1],col=2)
        points(SR3[(Thalf+window):(nw-window-2),1],col=3)
        points(SRrrr[(Thalf+window):(nw-window-2),1],col=4)
        
        
        
        # construct new colors - alpha gives the strength of the color
        c1 <- rgb(173,216,230, max = 255, alpha = 200, names = "lt.blue")
        c2 <- rgb(255,192,203, max = 255, alpha = 30, names = "lt.pink")
        c3 <- rgb(144,238,144, max = 255, alpha = 100, names = "lt.green")
        c4 <- rgb(255,0,0, max = 255, alpha = 80, names = "lt.red")
        
        
        hSR <- hist(SR[(Thalf+window):(nw-window-2),1], breaks = 30 , plot = FALSE)
        hSR2 <- hist(SR2[(Thalf+window):(nw-window-2),1], breaks = 30 , plot = FALSE)
        hSR3 <- hist(SR3[(Thalf+window):(nw-window-2),1] , breaks = 30 , plot = FALSE)
        
        pdf('SR_K1_9Jan23.pdf')
        plot(hSR,  main="
            Portfolio Performance: Sharpe Ratio - Tangency Portolios
\n   Out-of-Sample: local PCA  vs. constant PCA  (T=24) \n" ,xlim=c(-3,3),ylim=c(0,25),
             xlab="Out-of-Sample Sharpe Ratio (p.a.)",col = c1) # Plot 1st histogram using a transparent color
        plot(hSR2,  col = c3, add = TRUE) # Add 2nd histogram using different color
        abline(v=0.579,col=2,lwd=5)
        legend(-3,20,c("local PCA unb","local PCA bal", "constant PCA"), lwd=c(5,5), col=c(c1,c3,2), y.intersp=0.75)
        dev.off()
        
        hSR <- hist(SRrrrisvec[(Thalf+window):(nw-window-2),1], breaks = 30 , plot = FALSE)
        hSR2 <- hist(SRrrris2vec[(Thalf+window):(nw-window-2),1], breaks = 30 , plot = FALSE)
        hSR3 <- hist(SRrrris3vec[(Thalf+window):(nw-window-2),1] , breaks = 30 , plot = FALSE)
        
        
        
        
        pdf('SR_K1_is_9Jan23.pdf')
        plot(hSR,  main="
            Portfolio Performance: Sharpe Ratio - Tangency Portolios
\n In-Sample: local PCA  vs. constant PCA  (T=24) \n" ,xlim=c(-3,3),ylim=c(0,25),
             xlab="In-Sample Sharpe Ratio (p.a.)",col = c1) # Plot 1st histogram using a transparent color
        plot(hSR2,  col = c3, add = TRUE) # Add 2nd histogram using different color
        abline(v=0.797,col=2,lwd=5)
        legend(-3,20,c("local PCA unb","local PCA bal", "constant PCA"), lwd=c(5,5), col=c(c1,c3,2), y.intersp=0.75)
        dev.off()
        
        
        
        
        
        
        
        # k=3 
        
        
        
        hSR <- hist(SR[(Thalf+window):(nw-window-2),3], breaks = 30 , plot = FALSE)
        hSR2 <- hist(SR2[(Thalf+window):(nw-window-2),3], breaks = 30 , plot = FALSE)
        hSR3 <- hist(SR3[(Thalf+window):(nw-window-2),3] , breaks = 30 , plot = FALSE)
        
        
        pdf('SR_K3_9Jan23.pdf')
        plot(hSR,  main="
            Portfolio Performance: Sharpe Ratio - Tangency Portolios
\n Out-of-Sample: local PCA  vs. constant PCA  (T=24) \n" ,xlim=c(-3,3),ylim=c(0,25),
             xlab="Out-of-Sample Sharpe Ratio (p.a.)",col = c1) # Plot 1st histogram using a transparent color
        plot(hSR2,  col = c3, add = TRUE) # Add 2nd histogram using different color
        abline(v=0.127,col=2,lwd=5)
        legend(-3,20,c("local PCA unb","local PCA bal", "constant PCA"), lwd=c(5,5), col=c(c1,c3,2), y.intersp=0.75)
        dev.off()
        
        
        
        hSR <- hist(SRrrrisvec[(Thalf+window):(nw-window-2),3], breaks = 30 , plot = FALSE)
        hSR2 <- hist(SRrrris2vec[(Thalf+window):(nw-window-2),3], breaks = 30 , plot = FALSE)
        hSR3 <- hist(SRrrris3vec[(Thalf+window):(nw-window-2),3] , breaks = 30 , plot = FALSE)
        
        
        
        
        pdf('SR_K3_is_9Jan23.pdf')
        plot(hSR,  main="
            Portfolio Performance: Sharpe Ratio - Tangency Portolios
\n In-Sample: local PCA  vs. constant PCA  (T=24) \n" ,xlim=c(-3,3),ylim=c(0,30),
             xlab="In-Sample Sharpe Ratio (p.a.)",col = c1) # Plot 1st histogram using a transparent color
        plot(hSR2,  col = c3, add = TRUE) # Add 2nd histogram using different color
        abline(v=0.948,col=2,lwd=5)
        legend(-2,20,c("local PCA unb","local PCA bal", "constant PCA"), lwd=c(5,5), col=c(c1,c3,2), y.intersp=0.75)
        dev.off()
        
        
        
        
        
        
        # k=7   
        
        hSR <- hist(SR[(Thalf+window):(nw-window-2),7], breaks = 30 , plot = FALSE)
        hSR2 <- hist(SR2[(Thalf+window):(nw-window-2),7], breaks = 30 , plot = FALSE)
        hSR3 <- hist(SR3[(Thalf+window):(nw-window-2),7] , breaks = 30 , plot = FALSE)
        
        
        pdf('SR_K7_9Jan23.pdf')
        plot(hSR,  main="
            Portfolio Performance: Sharpe Ratio - Tangency Portolios
\n  Out-of-Sample: local PCA  vs. constant PCA  (T=24) \n" ,xlim=c(-3,3),ylim=c(0,25),
             xlab="Out-of-Sample: Sharpe Ratio (p.a.)",col = c1) # Plot 1st histogram using a transparent color
        plot(hSR2,  col = c3, add = TRUE) # Add 2nd histogram using different color
        abline(v= -0.294,col=2,lwd=5)
        
        legend(-3,20,c("local PCA unb","local PCA bal", "constant PCA"), lwd=c(5,5), col=c(c1,c3,2), y.intersp=0.75)
        dev.off()
        
        
        
        hSR <- hist(SRrrrisvec[(Thalf+window):(nw-window-2),7], breaks = 30 , plot = FALSE)
        hSR2 <- hist(SRrrris2vec[(Thalf+window):(nw-window-2),7], breaks = 30 , plot = FALSE)
        hSR3 <- hist(SRrrris3vec[(Thalf+window):(nw-window-2),7] , breaks = 30 , plot = FALSE)
        
        
        
        
        pdf('SR_K7_is_9Jan23.pdf')
        plot(hSR,  main="
            Portfolio Performance: Sharpe Ratio - Tangency Portolios
\n In-Sample: local PCA  vs. constant PCA  (T=24) \n" ,xlim=c(-5,5),ylim=c(0,30),
             xlab="In-Sample Sharpe Ratio (p.a.)",col = c1) # Plot 1st histogram using a transparent color
        plot(hSR2,  col = c3, add = TRUE) # Add 2nd histogram using different color
        abline(v= 1.069,col=2,lwd=5)
        legend(-2,20,c("local PCA unb","local PCA bal", "constant PCA"), lwd=c(5,5), col=c(c1,c3,2), y.intersp=0.75)
        dev.off()
        
        
        
        
        #k=10
        
        
        
        
        
        hSR <- hist(SR[(Thalf+window):(nw-window-2),10], breaks = 30 , plot = FALSE)
        hSR2 <- hist(SR2[(Thalf+window):(nw-window-2),10], breaks = 30 , plot = FALSE)
        hSR3 <- hist(SR3[(Thalf+window):(nw-window-2),10] , breaks = 30 , plot = FALSE)
        
        
        pdf('SR_K10_9Jan23.pdf')
        plot(hSR,  main="
            Portfolio Performance: Sharpe Ratio - Tangency Portolios
\n Out-of-Sample: local PCA  vs. constant PCA  (T=24) \n" ,xlim=c(-3,3),ylim=c(0,25),
             xlab="Out-of-Sample Sharpe Ratio (p.a.)",col = c1) # Plot 1st histogram using a transparent color
        plot(hSR2,  col = c3, add = TRUE) # Add 2nd histogram using different color
        abline(v=  -0.279,col=2,lwd=5)
        legend(-3,20,c("local PCA unb","local PCA bal", "constant PCA"), lwd=c(5,5), col=c(c1,c3,2), y.intersp=0.75)
        dev.off()
        
        
        hSR <- hist(SRrrrisvec[(Thalf+window):(nw-window-2),10], breaks = 30 , plot = FALSE)
        hSR2 <- hist(SRrrris2vec[(Thalf+window):(nw-window-2),10], breaks = 30 , plot = FALSE)
        hSR3 <- hist(SRrrris3vec[(Thalf+window):(nw-window-2),10] , breaks = 30 , plot = FALSE)
        
        
        
        
        pdf('SR_K10_is_9Jan23.pdf')
        plot(hSR,  main="
            Portfolio Performance: Sharpe Ratio - Tangency Portolios
\n In-Sample: local PCA  vs. constant PCA  (T=24) \n" ,xlim=c(-5,5),ylim=c(0,30),
             xlab="In-Sample Sharpe Ratio (p.a.)",col = c1) # Plot 1st histogram using a transparent color
        plot(hSR2,  col = c3, add = TRUE) # Add 2nd histogram using different color
        abline(v= 1.092,col=2,lwd=5)
        legend(-3,20,c("local PCA unb","local PCA bal", "constant PCA"), lwd=c(5,5), col=c(c1,c3,2), y.intersp=0.75)
        dev.off()
        
        
        
        
  
     
  
  rm(k0, i, supp, time, delete, X)
}


# Canonical  Correlations
{
  
  
  
   

  time = Sys.time()
  
  k0 = c(1, 3, 5, 7, 10)
  PP.lambda =  PP.lambdau = PP.lambda.nodemean =  PP.lambdau.nodemean =  PP.beta = matrix(NA, nrow = length(k0) + 1, ncol = nw)
  
  PP.lambdaFF =  PP.lambdauFF = PP.lambdaFF.nodemean =  PP.lambdauFF.nodemean  = matrix(NA, nrow =1 , ncol = nw)
  
  PP.lambdau2 =  PP.lambdau3 = PP.lambdau2.nodemean =  PP.lambdau3.nodemean =   matrix(NA, nrow = length(k0) + 1, ncol = nw)
  PP.testdelta = PP.testdelta2=  PP.testdelta_neg = PP.testdelta_pos =   matrix(NA, nrow = length(k0) + 1, ncol = nw)
  hj_cond=hj_uncond=mean_acondFF=mean_auncondFF=mean_condFF=mean_uncondFF=SS_condFF=SS_uncondFF=mean_acond=mean_auncond=mean_cond=mean_uncond=SS_cond=SS_uncond=matrix(NA, nrow = nw, ncol = 1)
  
  TT=window
  
  
  
  rownames(PP.lambda) = rownames(PP.beta) = c("k = hat(k)", paste("k =", k0))
  colnames(PP.lambda) = colnames(PP.beta) = as.character(ts.enddate)
  
   NN=ncol(data)
   lambdau.supp= matrix(NA, nrow = NN, ncol = 10)
   lambdau.suppFF= matrix(NA, nrow = NN, ncol = 5)
 
   NN=ncol(data)
  for (i in 1:NN)
  {
    X_i_full = data[,i]             # vettore lungo 648 relativo all'asset i
    i_na = is.na(X_i_full)    # posizioni del vettore X_i_full dove ci sono valori mancanti
    X_i = X_i_full[!i_na]               # vettore esclusi i valori mancanti (le osservazioni potrebbero non essere consecutive)
    F_i =  ts.factorsu[!i_na,,1]                # factors ai soli timesteps dove sono presenti osservazioni per X_i
    lambdau.supp[i,] = t(X_i) %*% F_i / length(X_i)
    
    
    X_i_fullFF = data[43:480,i] #data[43:648,i]             # vettore lungo 648 relativo all'asset i
    i_naFF = is.na(X_i_fullFF)    # posizioni del vettore X_i_full dove ci sono valori mancanti
    X_iFF = X_i_fullFF[!i_naFF]               # vettore esclusi i valori mancanti (le osservazioni potrebbero non essere consecutive)
    FFF=FF[43:480,]
    F_iFF =  FFF[!i_naFF,]                # factors ai soli timesteps dove sono presenti osservazioni per X_i
    summ=sum(!i_naFF)
    if (summ<6)
    {lambdau.suppFF[i,] = matrix(NA,1,5)}
    else
    {      aa=length(X_iFF)
    MM=(diag(1,aa,aa)-(1/aa)*matrix(1,aa,1)%*%matrix(1,1,aa)  )
    lambdau.suppFF[i,] = t(X_iFF) %*% MM %*% F_iFF %*% solve(t(F_iFF)%*% MM %*% F_iFF)}
    
  }
  
  
  risk_premiau.supp=  apply(ts.factorsu, 2, mean)
  omegau.supp=cov(matrix(ts.factorsu[,,1],ncol=10))
  
  risk_premiauFF.supp=  apply(FF[43:480,], 2, mean)
  omegau.supp=cov(FF[43:480,])
  
  
  i_naLL = is.na(lambdau.suppFF)
  meanlamFF=100*apply(matrix(lambdau.suppFF[!i_naLL],ncol=5), 2, mean)
  
  covlamFF=100*100*cov(matrix(lambdau.suppFF[!i_naLL],ncol=5))
  
  
  lambda.suppbal1= matrix(0, nrow = NN, ncol = 10)
  lambda.suppbalt= matrix(0, nrow = NN, ncol = 10)
  
  loadingsTrot=array(0,c(648,NN,10))
  loadingsTrot_Markus=array(0,c(480,NN,10))
  loadingsTrot_Markus2=array(0,c(480,NN,10))
  loadingsTrot_Markus3=array(0,c(480,NN,10))
  
  
  ei10=matrix(0,nrow=nw,ncol=10)
  ei10M=matrix(0,nrow=nw,ncol=10)
  ei10M2=matrix(0,nrow=nw,ncol=10)
  ei10M3=matrix(0,nrow=nw,ncol=10)
  ei5=matrix(0,nrow=nw,ncol=5)
  ei3=matrix(0,nrow=nw,ncol=3)
  
  ei10f=matrix(0,nrow=nw,ncol=10)
  ei10fM=matrix(0,nrow=nw,ncol=10)
  ei10FF=matrix(0,nrow=nw,ncol=10)
  ei5f=matrix(0,nrow=nw,ncol=5)
  ei3f=matrix(0,nrow=nw,ncol=3)
  ei5fM=matrix(0,nrow=nw,ncol=5)
  ei3fM=matrix(0,nrow=nw,ncol=3)
  
  
  eis=matrix(0,nrow=nw,ncol=10)
  eisall=matrix(0,nrow=nw,ncol=1)
  eis_dy=matrix(0,nrow=nw,ncol=1)
  eis_defsp=matrix(0,nrow=nw,ncol=1)
  eis_termsp=matrix(0,nrow=nw,ncol=1)
  eis_cape=matrix(0,nrow=nw,ncol=1)
  eis_SP=matrix(0,nrow=nw,ncol=1)
  

  
  
  
  factorsTrot=matrix(NA,nrow=480,ncol=10)
  factorT=matrix(NA,nrow=480,ncol=10)
  factorsTrot_Markus=matrix(NA,nrow=480,ncol=10)
  factorsTrot_Markus2=matrix(NA,nrow=480,ncol=10)
  factorsTrot_Markus3=matrix(NA,nrow=480,ncol=10)
  
  
  # here GC loadings below GC factors
  for (i in 1:nw)
  {
    index = i:(i + window - 1)
    X = data[index, ]
    
    if (!is.na(ts.deletestocks[[1]][1]))  
    {
      delete = ts.deletestocks[[i]]
      if (class(delete) != "NULL") X = X[,-delete]
      if (class(delete) != "NULL") lambdau=lambdau.supp[-delete,]
      if (class(delete) != "NULL") lambdauFF=lambdau.suppFF[-delete,]
      

      
      
    }
    X = as.matrix(X)
    N = ncol(X)
    rf.supp = rf[index] - 1
    X = X - rf.supp %*% t(rep(1, N))
    
    X_mean = apply(X, 2, mean)
    
    factor.supp = ts.factors[,,i]
    lambda.supp = t(1/window *(t(factor.supp) %*% X))
    if(i==1){lambda.suppbal1[-delete,]=lambda.supp}
    lambda.suppbalt[-delete,]=lambda.supp
    HH=t(lambda.suppbal1)%*%lambda.suppbalt%*%solve(t(lambda.suppbalt)%*%lambda.suppbalt)
    factor.supp_rot=factor.supp%*%solve(HH)
    print(HH[1,1])
    print(factor.supp_rot[1,1])
    
   
    factorsTrot[i+window-1,]=factor.supp_rot[window,]
    factorT[i+window-1,]=ts.factors[window,,i]
   
  
     lambda.supp_rot=lambda.supp%*%t(HH)
     loadingsTrot[i+window-1,-delete,]=lambda.supp_rot   

    
    supp=svd((t(factor.supp)%*%ts.factorsu[index,,1]), nu =10, nv = 10)#, LINPACK = FALSE)

    HHM=supp[[2]]%*%t(supp[[3]])
    factor.supp_rot_Markus=factor.supp%*%(HHM)
    factorsTrot_Markus[i+window-1,]=factor.supp_rot_Markus[window,]
    
    supp2=matrix(0,10,10)
    supp2=svd((t(lambda.supp)%*%lambdau.supp[-delete,]), nu =10, nv = 10)#, LINPACK = FALSE)

    HHM2=supp2[[2]]%*%t(supp2[[3]])
    lambda.supp_rot_Markus=lambda.supp%*%(HHM2)
    loadingsTrot_Markus[i-1+window,-delete,]=lambda.supp%*%(HHM2)
    
    
    loadingsTrot_Markus2[i+window-1,-delete,]=lambda.supp%*%t(solve(HHM))
    
    loadingsTrot_Markus3[i+window-1,-delete,]=lambda.supp%*%HHM
    
    
    
    
    
    
    NAA=nrow(lambda.supp_rot)
    A=matrix(0,NAA,10)
 A=lambda.supp_rot - matrix(1,nrow=NAA,ncol=1)%*%apply(lambda.supp_rot,2,mean)
 B=matrix(0,NAA,10)
 B=lambdau.supp[-delete,]- matrix(1,nrow=NAA,ncol=1)%*%apply(lambdau.supp[-delete,],2,mean)
 GC10=matrix(0,10,10)
 GC10=(solve(t(A)%*%A))%*%(t(A)%*%B)%*%(solve(t(B)%*%B))%*%t(B)%*%A
 aa=matrix(0,10,1) 
 aa=eigen(GC10, only.values = TRUE)
  ei10[i,]=aa[[1]]
  

  NAA=nrow(lambda.supp_rot)
  A1=matrix(0,NAA,10)
  A1=loadingsTrot_Markus[i+window-1,-delete,] - matrix(1,nrow=NAA,ncol=1)%*%apply(loadingsTrot_Markus[i+window-1,-delete,],2,mean)
  B=lambdau.supp[-delete,]- matrix(1,nrow=NAA,ncol=1)%*%apply(lambdau.supp[-delete,],2,mean)
  GC10M=matrix(0,10,10)
  GC10M=(solve(t(A1)%*%A1))%*%(t(A1)%*%B)%*%(solve(t(B)%*%B))%*%t(B)%*%A1
  aa1=matrix(0,10,1) 
  aa1=eigen(GC10M, only.values = TRUE)
  ei10M[i,]=aa1[[1]]
  
  
  
  NAA=nrow(lambda.supp_rot)
  A2=matrix(0,NAA,10)
  A2=loadingsTrot_Markus2[i+window-1,-delete,] - matrix(1,nrow=NAA,ncol=1)%*%apply(loadingsTrot_Markus2[i+window-1,-delete,],2,mean)
  B=lambdau.supp[-delete,]- matrix(1,nrow=NAA,ncol=1)%*%apply(lambdau.supp[-delete,],2,mean)
  GC10M2=matrix(0,10,10)
  GC10M2=(solve(t(A2)%*%A2))%*%(t(A2)%*%B)%*%(solve(t(B)%*%B))%*%t(B)%*%A2
  aa2=matrix(0,10,1) 
  aa2=eigen(GC10M2, only.values = TRUE)
  ei10M2[i,]=aa2[[1]]
  
  
  NAA=nrow(lambda.supp_rot)
  A3=matrix(0,NAA,10)
  A3=loadingsTrot_Markus3[i+window-1,-delete,] - matrix(1,nrow=NAA,ncol=1)%*%apply(loadingsTrot_Markus3[i+window-1,-delete,],2,mean)
  B=lambdau.supp[-delete,]- matrix(1,nrow=NAA,ncol=1)%*%apply(lambdau.supp[-delete,],2,mean)
  GC10M3=matrix(0,10,10)
  GC10M3=(solve(t(A3)%*%A3))%*%(t(A3)%*%B)%*%(solve(t(B)%*%B))%*%t(B)%*%A3
  aa3=matrix(0,10,1) 
  aa3=eigen(GC10M3, only.values = TRUE)
  ei10M3[i,]=aa3[[1]]
  
  
  
  
  
    
 A3=loadingsTrot_Markus[i+window-1,-delete,1:3] - matrix(1,nrow=NAA,ncol=1)%*%apply(loadingsTrot_Markus3[i+window-1,-delete,1:3],2,mean)
 
 
 B3=lambdau.supp[-delete,1:3]- matrix(1,nrow=NAA,ncol=1)%*%apply(lambdau.supp[-delete,1:3],2,mean)
 
 
 GC3=(solve(t(A3)%*%A3))%*%(t(A3)%*%B3)%*%(solve(t(B3)%*%B3))%*%t(B3)%*%A3
 aa=eigen(GC3, only.values = TRUE)
 ei3[i,]=aa[[1]]
 
 
  A5=loadingsTrot_Markus[i+window-1,-delete,1:5] - matrix(1,nrow=NAA,ncol=1)%*%apply(loadingsTrot_Markus3[i+window-1,-delete,1:5],2,mean)
 B5=lambdau.supp[-delete,1:5]- matrix(1,nrow=NAA,ncol=1)%*%apply(lambdau.supp[-delete,1:5],2,mean)
 
 GC5=(solve(t(A5)%*%A5))%*%(t(A5)%*%B5)%*%(solve(t(B5)%*%B5))%*%t(B5)%*%A5
 aa=eigen(GC5, only.values = TRUE)
 ei5[i,]=aa[[1]]
 
 
 
 Af=matrix(0,window,10) 
 Af=factor.supp_rot- matrix(1,nrow=window,ncol=1)%*%apply(factor.supp_rot,2,mean)
 Bf=matrix(0,window,10) 
 Bf=ts.factorsu[index,,1]- matrix(1,nrow=window,ncol=1)%*%apply(ts.factorsu[index,,1],2,mean)
 GC10f=matrix(0,nrow=10,ncol=10)
 GC10f=(solve(t(Af)%*%Af))%*%(t(Af)%*%Bf)%*%(solve(t(Bf)%*%Bf))%*%t(Bf)%*%Af
 aaf=matrix(0,10,1) 
  aaf=eigen(GC10f, only.values = TRUE)
 ei10f[i,]=aaf[[1]]
 
 Af1=matrix(0,window,10) 
 Af1=factor.supp_rot_Markus- matrix(1,nrow=window,ncol=1)%*%apply(factor.supp_rot_Markus,2,mean)
 Bf=ts.factorsu[index,,1]- matrix(1,nrow=window,ncol=1)%*%apply(ts.factorsu[index,,1],2,mean)
 GC10fM=matrix(0,nrow=10,ncol=10)
 GC10fM=(solve(t(Af1)%*%Af1))%*%(t(Af1)%*%Bf)%*%(solve(t(Bf)%*%Bf))%*%t(Bf)%*%Af1
 aaf1=matrix(0,10,1) 
 aaf1=eigen(GC10fM, only.values = TRUE)
 ei10fM[i,]=aaf1[[1]]
 
 
 Af1=matrix(0,window,10) 
 Af1=factor.supp_rot_Markus- matrix(1,nrow=window,ncol=1)%*%apply(factor.supp_rot_Markus,2,mean)
 Bf=FF[index,]- matrix(1,nrow=window,ncol=1)%*%apply(FF[index,],2,mean)
 GC10fM=matrix(0,nrow=10,ncol=10)
 GC10fM=(solve(t(Af1)%*%Af1))%*%(t(Af1)%*%Bf)%*%(solve(t(Bf)%*%Bf))%*%t(Bf)%*%Af1
 aaf1=matrix(0,10,1) 
 if(i >=43)
{ aaf1=eigen(GC10fM, only.values = TRUE)}
 ei10FF[i,]=aaf1[[1]]
 

 

 A3=factor.supp_rot[,1:3]- matrix(1,nrow=window,ncol=1)%*%apply(factor.supp_rot[,1:3],2,mean)
 B3=ts.factorsu[index,1:3,1]- matrix(1,nrow=window,ncol=1)%*%apply(ts.factorsu[index,1:3,1],2,mean)
 GC3=matrix(0,nrow=3,ncol=3)
 GC3=(solve(t(A3)%*%A3))%*%(t(A3)%*%B3)%*%(solve(t(B3)%*%B3))%*%t(B3)%*%A3
 aa=eigen(GC3, only.values = TRUE)
 ei3f[i,]=aa[[1]]
 
 A3=factor.supp_rot_Markus[,1:3]- matrix(1,nrow=window,ncol=1)%*%apply(factor.supp_rot_Markus[,1:3],2,mean)
 B3=ts.factorsu[index,1:3,1]- matrix(1,nrow=window,ncol=1)%*%apply(ts.factorsu[index,1:3,1],2,mean)
 GC3=matrix(0,nrow=3,ncol=3)
 GC3=(solve(t(A3)%*%A3))%*%(t(A3)%*%B3)%*%(solve(t(B3)%*%B3))%*%t(B3)%*%A3
 aa=eigen(GC3, only.values = TRUE)
 ei3fM[i,]=aa[[1]]
 
 
 

 A5=factor.supp_rot[,1:5]- matrix(1,nrow=window,ncol=1)%*%apply(factor.supp_rot[,1:5],2,mean)
 B5=ts.factorsu[index,1:5,1]- matrix(1,nrow=window,ncol=1)%*%apply(ts.factorsu[index,1:5,1],2,mean)
 GC5=matrix(0,nrow=5,ncol=5)
 GC5=(solve(t(A5)%*%A5))%*%(t(A5)%*%B5)%*%(solve(t(B5)%*%B5))%*%t(B5)%*%A5
 aa=eigen(GC5, only.values = TRUE)
 ei5f[i,]=aa[[1]]
 
 A5=factor.supp_rot_Markus[,1:5]- matrix(1,nrow=window,ncol=1)%*%apply(factor.supp_rot_Markus[,1:5],2,mean)
 B5=ts.factorsu[index,1:5,1]- matrix(1,nrow=window,ncol=1)%*%apply(ts.factorsu[index,1:5,1],2,mean)
 GC5=matrix(0,nrow=5,ncol=5)
 GC5=(solve(t(A5)%*%A5))%*%(t(A5)%*%B5)%*%(solve(t(B5)%*%B5))%*%t(B5)%*%A5
 aa=eigen(GC5, only.values = TRUE)
 ei5fM[i,]=aa[[1]]
 
 
 
  }
  

  
  
    
    # PLOT GC
    
    
     
     
  
  pdf('T120_GC_FACTORS_17Jan23.pdf')
  plot(x=ts.enddate[241:TTT],y=gc[241:TTT,1],type="l",ylim=c(0,1),
       main="Canonical Correlations: Factors \n local PCA vs constant PCA (T=120)" ,
       xlab="Date",ylab="Canonical Correlation"
  ) # Add 2nd histogram using different color
  lines(x=ts.enddate[241:TTT],y=gc[241:TTT,2],col=2)
  lines(x=ts.enddate[241:TTT],y=gc[241:TTT,3],col=3)
  lines(x=ts.enddate[241:TTT],y=gc[241:TTT,4],col=4)
  lines(x=ts.enddate[241:TTT],y=gc[241:TTT,5],col=5)
  abline(v = rec[which(rec > 0)], col = "grey80")
  legend(ts.enddate[241],0.6,c("gc=1", "gc=2","gc=3","gc=4","gc=5","econ-fin crises"), lwd=c(5,5), col=c(1,2,3,4,5,"grey"), y.intersp=0.75)
  dev.off()
  
  
    
  
   
A=matrix(NA,nw,10)
  A=factorsTrot_Markus[window:648,]- matrix(1,nrow=nw,ncol=1)%*%apply(factorsTrot_Markus[window:648,],2,mean)
    B=ts.factorsu[window:648,,1]-matrix(1,nrow=nw,ncol=1)%*%apply(ts.factorsu[window:648,,1],2,mean)
    GCu=(solve(t(A)%*%A))%*%(t(A)%*%B)%*%(solve(t(B)%*%B))%*%t(B)%*%A
    aa=eigen(GCu, only.values = TRUE)
    aa[[1]]
    
    
    A=matrix(NA,nw,10)
    if (window >44)
{    A=factorsTrot_Markus[window:648,]- matrix(1,nrow=nw,ncol=1)%*%apply(factorsTrot_Markus[window:648,],2,mean)
    B=matrix(NA,nw,5)
    B=FF[window:648,]-matrix(1,nrow=nw,ncol=1)%*%apply(FF[window:648,],2,mean)} else {
      uuu=648-44+1
      A=factorsTrot_Markus[44:648,]- matrix(1,nrow=uuu,ncol=1)%*%apply(factorsTrot_Markus[44:648,],2,mean)
      B=matrix(NA,nw,5)
      B=FF[44:648,]-matrix(1,nrow=uuu,ncol=1)%*%apply(FF[44:648,],2,mean)   
          }
    GCFF=(solve(t(A)%*%A))%*%(t(A)%*%B)%*%(solve(t(B)%*%B))%*%t(B)%*%A
    bb=eigen(GCFF, only.values = TRUE)
    bb[[1]]

    
    
        
   #  rolling regression of average loadings with state variables
    
   # dy  defsp   termsp  cape SP
   toler=1e-75
    for (i in window:(nw-window))
    {
      index = i:(i + window - 1)
      X = data[index, ]
      
      if (!is.na(ts.deletestocks[[1]][1]))  
      {
        delete = ts.deletestocks[[i]]
        if (class(delete) != "NULL") X = X[,-delete]
        if (class(delete) != "NULL") lambdau=lambdau.supp[-delete,]
        if (class(delete) != "NULL") lambdauFF=lambdau.suppFF[-delete,]
        

        
      }
      X = as.matrix(X)
      N = ncol(X)
      rf.supp = rf[index] - 1
      X = X - rf.supp %*% t(rep(1, N))
      
      X_mean = apply(X, 2, mean)
      
      
      aveload=apply(loadingsTrot_Markus[index,-delete,],c(1,3),mean) #  ave wrt to 2nd dim keep 1,3 fixed
      aveaveload=apply(aveload,1,mean) #  ave wrt to 2nd dim keep 1,3 fixed
      
      
       {
      A=matrix(0,window,10)
       A=(aveload- matrix(1,nrow=window,ncol=1)%*%apply(aveload,2,mean))
       B=matrix(0,window,5)
             statev=cbind(dy[index],termsp[index],cape[index],SP[index])

              B=statev- matrix(1,nrow=window,ncol=1)%*%apply(statev,2,mean)
       GCs=matrix(0,10,10)
       pippo=det(t(A)%*%A)
       if (pippo<toler)
       {eis[i,]=matrix(0,1,10)} else
       {       
       GCs=(solve(t(A)%*%A))%*%(t(A)%*%B)%*%(solve(t(B)%*%B))%*%t(B)%*%A
       aa=matrix(0,10,1) 
       aa=eigen(GCs, only.values = TRUE)
       eis[i,]=Re(aa[[1]])}
      }
       
       
      {
       A=matrix(0,window,10)
       A=aveload- matrix(1,nrow=window,ncol=1)%*%apply(aveload,2,mean)
       B=matrix(0,window,1)
       statev=dy[index]
       B=statev- matrix(1,nrow=window,ncol=1)%*%mean(statev)
       GCs=matrix(0,10,10)
       pippo=det(t(A)%*%A)
       if (pippo<toler)
       {eis_dy[i,]=matrix(0,1,1)} else
       {
         GCs=(solve(t(A)%*%A))%*%(t(A)%*%B)%*%(solve(t(B)%*%B))%*%t(B)%*%A
         aa=matrix(0,10,1)
         aa=eigen(GCs, only.values = TRUE)
         eis_dy[i,]=Re(aa[[1]][[1]])}
}

      
      
      {
       A=matrix(0,window,10)
       A=aveload- matrix(1,nrow=window,ncol=1)%*%apply(aveload,2,mean)
       B=matrix(0,window,1)
       statev=termsp[index]
       B=statev- matrix(1,nrow=window,ncol=1)%*%mean(statev)
       GCs=matrix(0,10,10)
       pippo=det(t(A)%*%A)
       if (pippo<toler)
       {eis_termsp[i,]=matrix(0,1,1)} else
       {
         GCs=(solve(t(A)%*%A))%*%(t(A)%*%B)%*%(solve(t(B)%*%B))%*%t(B)%*%A
         aa=matrix(0,10,1)
         aa=eigen(GCs, only.values = TRUE)
         eis_termsp[i,]=Re(aa[[1]][[1]])}
      }


      {        
       A=matrix(0,window,10)
       A=aveload- matrix(1,nrow=window,ncol=1)%*%apply(aveload,2,mean)
       B=matrix(0,window,1)
       statev=cape[index]
       B=statev- matrix(1,nrow=window,ncol=1)%*%mean(statev)
       GCs=matrix(0,10,10)
       pippo=det(t(A)%*%A)
       if (pippo<toler)
       {eis_cape[i,]=matrix(0,1,1)} else
       {
         GCs=(solve(t(A)%*%A))%*%(t(A)%*%B)%*%(solve(t(B)%*%B))%*%t(B)%*%A
         aa=matrix(0,10,1)
         aa=eigen(GCs, only.values = TRUE)
         eis_cape[i,]=Re(aa[[1]][[1]])}
}

{       {A=matrix(0,window,10)
       A=aveload- matrix(1,nrow=window,ncol=1)%*%apply(aveload,2,mean)
       B=matrix(0,window,1)
       statev=SP[index]
       B=statev- matrix(1,nrow=window,ncol=1)%*%mean(statev)
       GCs=matrix(0,10,10)
       pippo=det(t(A)%*%A)
       if (pippo<toler)
       {eis_SP[i,]=matrix(0,1,1)} else
       {
         GCs=(solve(t(A)%*%A))%*%(t(A)%*%B)%*%(solve(t(B)%*%B))%*%t(B)%*%A
         aa=matrix(0,10,1)
         aa=eigen(GCs, only.values = TRUE)
         eis_SP[i,]=Re(aa[[1]][[1]])}}
}
      
 
        }
     

   
   # R-squared
   
       r2=matrix(0,nw,1)
       r2u=matrix(0,nw,1)
              r2FF=matrix(0,nw,1)
       
       
       for (i in window:nw)
       {
         index = i:(i + window - 1)
         X = data[index, ]
         
         if (!is.na(ts.deletestocks[[1]][1]))  
         {
           delete = ts.deletestocks[[i]]
           if (class(delete) != "NULL") X = X[,-delete]
           if (class(delete) != "NULL") lambdau=lambdau.supp[-delete,]
           if (class(delete) != "NULL") lambdauFF=lambdau.suppFF[-delete,]
           
           
           
         }
         X = as.matrix(X)
         N = ncol(X)
         rf.supp = rf[index] - 1
         X = X - rf.supp %*% t(rep(1, N))
         
         X_mean = apply(X, 2, mean)
         
         cond.lm = lm(X ~ factorsTrot_Markus[index,])
         aa=summary(cond.lm)
         
         condu.lm = lm(X ~ ts.factorsu[index,,1])
         aau=summary(condu.lm)
         
         
         condFF.lm = lm(X ~ FF[index,])
         aaFF=summary(condFF.lm)
         
         
         
         
         
         
         j=1
         for (j in 1:N)
         {         r2[i]=r2[i]+aa[[j]][[8]]
         r2u[i]=r2u[i]+aau[[j]][[8]]
         
         
         r2FF[i]=r2FF[i]+aaFF[[j]][[8]]
         
         
         }
         r2[i]=r2[i]/N
         r2u[i]=r2u[i]/N
         r2FF[i]=r2FF[i]/N
         
       }  

              N=ncol(data)
       r2ufull=0
       r2FFfull=0
       dataFF=data[44:480,]
       
       
         j=1
         for (j in 1:N)
         {        
           
           index=which(is.na(data[,j]))
           indexFF5=which(is.na(FF[,5]))
           indexFF4=which(is.na(FF[,4]))
           indexx=which(is.na(FF[,4])|is.na(data[,j]))
           

           X = data[-indexx,j]

           X = as.matrix(X) 
           TT = nrow(X)
           rf.supp = rf[-indexx] - 1
           X = X - rf.supp 
           if (nrow(X)>11) {
           condu.lm = lm(X ~ ts.factorsu[-indexx,,1])
           aau=summary(condu.lm)
           
           
              r2ufull=r2ufull+aau[[8]]
           }
         
           
           if (nrow(X)>11) {
             condFF.lm = lm(X ~ FF[-indexx,])
             aaFF=summary(condFF.lm)
             
             
             r2FFfull=r2FFfull+aaFF[[8]]
           }
           
           
         }
         r2ufull=r2ufull/N
         r2FFfull=r2FFfull/N         
  
           
       # variance decomposition
         
        con=matrix(0,nw,11)
        con_n=matrix(0,nw,11)   
        
            con_lam=matrix(0,nw,11)
        con_sig=matrix(0,nw,11)
        
        con_lam_n=matrix(0,nw,11)
        con_sig_n=matrix(0,nw,11)
        
        
        
        conFF=matrix(0,nw,5)
        conFF_n=matrix(0,nw,5)   
        
        conFF_lam=matrix(0,nw,5)
        conFF_sig=matrix(0,nw,5)
        
        conFF_lam_n=matrix(0,nw,5)
        conFF_sig_n=matrix(0,nw,5)
        
        
        
        for (i in 43:nw)
        {
          
          
          
          index = i:(i + window - 1)
          X = data[index, ]
          if (!is.na(ts.deletestocks[[1]][1]))  
          {
            delete = ts.deletestocks[[i]]
            if (class(delete) != "NULL") X = X[,-delete]
            if (class(delete) != "NULL") lambdau=lambdau.supp[-delete,]
            if (class(delete) != "NULL") lambdauFF=lambdau.suppFF[-delete,]
            
            
            
          }
          X = as.matrix(X)
          N = ncol(X)
          rf.supp = rf[index] - 1
          X = X - rf.supp %*% t(rep(1, N))
          
          
          
          
          factor.supp = ts.factors[,,i]
          lambda.supp = t(1/window *(t(factor.supp) %*% X))
          
          
          j=1;
          
          for (j in 1:10)
{ 
            
            sigmaf=var(factor.supp[,j])
        con[i,j]= (t(lambda.supp[,j])%*%lambda.supp[,j])*sigmaf/N
        con_lam[i,j]= (t(lambda.supp[,j])%*%lambda.supp[,j])/N
        con_sig[i,j]= sigmaf
          }
        
        
        
        MM=diag(1,nrow=window)-matrix(1,window,1)%*%matrix(1,1,window)/window
        lambda.suppFF =solve(t(FF[index,])%*%MM%*%FF[index,])%*%t(FF[index,])%*%MM%*%X 
        lambda.suppFF=as.matrix(lambda.suppFF,5,N)
       j=1
         for (j in 1:5)
        { 
          
        sigmaFF=var(FF[index,j])
        lll=matrix(lambda.suppFF[j,],N,1)
        conFF[i,j]= (t(lll)%*%lll)*sigmaFF/N
        conFF_lam[i,j]= (t(lll)%*%lll)/N
        conFF_sig[i,j]= sigmaFF
        
         }
        
        
        
        
        
        

            
      sigmaf=var(factor.supp[,ts.nfactorhat[i]])
    con[i,11]= (t(lambda.supp[,ts.nfactorhat[i]])%*%lambda.supp[,ts.nfactorhat[i]])*sigmaf/N
    con_lam[i,11]= (t(lambda.supp[,j])%*%lambda.supp[,j])/N
    con_sig[i,11]= sigmaf
    
            
          
        
        }
       
        j=1;
        
        for (j in 1:11)
        
        { con_n[,j]=con[,j]/mean(con[,j])
       
        con_lam_n[,j]=con_lam[,j]/mean(con_lam[,j])
        con_sig_n[,j]=con_sig[,j]/mean(con_sig[,j])
        }
        
        
        j=1;
        
        for (j in 1:5)
          
        { conFF_n[,j]=conFF[,j]/mean(conFF[,j])
        
        conFF_lam_n[,j]=conFF_lam[,j]/mean(conFF_lam[,j])
        conFF_sig_n[,j]=conFF_sig[,j]/mean(conFF_sig[,j])
        }
        
        con_n_ave=apply(con_n,1,mean)
        con_lam_n_ave=apply(con_lam_n,1,mean)
        con_sig_n_ave=apply(con_sig_n,1,mean)
        
        conFF_n_ave=apply(conFF_n,1,mean)
        conFF_lam_n_ave=apply(conFF_lam_n,1,mean)
        conFF_sig_n_ave=apply(conFF_sig_n,1,mean)

        

        # informal plots
        {
     
        con_n24 = read.csv("T24/con_T24.csv", stringsAsFactors = F)
        con_n120 = read.csv("T120/con_T120.csv", stringsAsFactors = F)
        conFF_n24 = read.csv("T24/conFF_T24.csv", stringsAsFactors = F)
        conFF_n120 = read.csv("T120/conFF_T120.csv", stringsAsFactors = F)
        
        
        aaa=seq(1:529)
#        ylim = c(0,1)
        ylim = c(min(con_n24),max(con_n24))
        #plot(x = ts.enddate, y = sdfT, type = "l", main = "Pricing performance PCA", xlab = "Date", ylab = "Pricing Performance", ylim = ylim)
        plot(x = ts.enddate, y =matrix(con_n24[97:625,1],ncol=1), type = "l", main = "Pricing performance PCA", xlab = "Date", ylab = "Pricing Performance", ylim = ylim)
        abline(v = rec[which(rec > 0)], col = "gray90")
        lines(x  =ts.enddate, y = conFF_n24[97:625,1],col=2)
        lines(x  =ts.enddate, y = 0.1*ts.nfactorhat,col=3) 
        
        
        con_n24 = read.csv("T24/con_n_T24.csv", stringsAsFactors = F)
        con_n120 = read.csv("T120/con_n_T120.csv", stringsAsFactors = F)
        conFF_n24 = read.csv("T24/conFF_n_T24.csv", stringsAsFactors = F)
        conFF_n120 = read.csv("T120/conFF_n_T120.csv", stringsAsFactors = F)
        
        
        aaa=seq(1:529)
        #        ylim = c(0,1)
        ylim = c(min(con_n24),max(con_n24))
        #plot(x = ts.enddate, y = sdfT, type = "l", main = "Pricing performance PCA", xlab = "Date", ylab = "Pricing Performance", ylim = ylim)
        plot(x = ts.enddate, y =matrix(con_n24[97:625,1],ncol=1), type = "l", main = "Pricing performance PCA", xlab = "Date", ylab = "Pricing Performance", ylim = ylim)
        abline(v = rec[which(rec > 0)], col = "gray90")
        lines(x  =ts.enddate, y = conFF_n24[97:625,1],col=2)
        lines(x  =ts.enddate, y = 0.1*ts.nfactorhat,col=3) 
        
        aaa=seq(1:529)
        #        ylim = c(0,1)
        ylim = c(min(con_n24),max(con_n24))
        #plot(x = ts.enddate, y = sdfT, type = "l", main = "Pricing performance PCA", xlab = "Date", ylab = "Pricing Performance", ylim = ylim)
        plot(x = ts.enddate, y =matrix(con_n120[97:625,1],ncol=1), type = "l", main = "Pricing performance PCA", xlab = "Date", ylab = "Pricing Performance", ylim = ylim)
        abline(v = rec[which(rec > 0)], col = "gray90")
        lines(x  =ts.enddate, y = conFF_n120[97:625,1],col=2)
        lines(x  =ts.enddate, y = 0.1*ts.nfactorhat,col=3) 
        
        
           
        
        con_n24 = read.csv("T24/con_lam_n_T24.csv", stringsAsFactors = F)
        con_n120 = read.csv("T120/con_lam_n_T120.csv", stringsAsFactors = F)
        conFF_n24 = read.csv("T24/conFF_lam_n_T24.csv", stringsAsFactors = F)
        conFF_n120 = read.csv("T120/conFF_lam_n_T120.csv", stringsAsFactors = F)
        
        
        aaa=seq(1:529)
        #        ylim = c(0,1)
        ylim = c(min(con_n24),max(con_n24))
        #plot(x = ts.enddate, y = sdfT, type = "l", main = "Pricing performance PCA", xlab = "Date", ylab = "Pricing Performance", ylim = ylim)
        plot(x = ts.enddate, y =matrix(con_n24[97:625,1],ncol=1), type = "l", main = "Pricing performance PCA", xlab = "Date", ylab = "Pricing Performance", ylim = ylim)
        abline(v = rec[which(rec > 0)], col = "gray90")
        lines(x  =ts.enddate, y = conFF_n24[97:625,1],col=2)
        lines(x  =ts.enddate, y = 0.1*ts.nfactorhat,col=3) 
        
        
        con_n24 = read.csv("T24/con_sig_n_T24.csv", stringsAsFactors = F)
        con_n120 = read.csv("T120/con_sig_n_T120.csv", stringsAsFactors = F)
        conFF_n24 = read.csv("T24/conFF_sig_n_T24.csv", stringsAsFactors = F)
        conFF_n120 = read.csv("T120/conFF_sig_n_T120.csv", stringsAsFactors = F)
        
        
        aaa=seq(1:529)
        #        ylim = c(0,1)
        ylim = c(min(con_n24),max(con_n24))
        #plot(x = ts.enddate, y = sdfT, type = "l", main = "Pricing performance PCA", xlab = "Date", ylab = "Pricing Performance", ylim = ylim)
        plot(x = ts.enddate, y =matrix(con_n24[97:625,1],ncol=1), type = "l", main = "Pricing performance PCA", xlab = "Date", ylab = "Pricing Performance", ylim = ylim)
        abline(v = rec[which(rec > 0)], col = "gray90")
        lines(x  =ts.enddate, y = conFF_n24[97:625,1],col=2)
        lines(x  =ts.enddate, y = con_n120[,1],col=4)
        lines(x  =ts.enddate, y = conFF_n120[,1],col=5)  
        lines(x  =ts.enddate, y = 0.1*ts.nfactorhat,col=3) 
        
        
        
}
        
        
         
         
       }
    
    
 
#    empirics  locally strong factors
#   see how the HKM factor is related to local PCA:
HKM = read.csv("He_Kelly_Manela_Factors_monthly.csv", header = T)


attach(HKM)
names(HKM)
View(HKM)
# 1  - Jan 1970
# 121 - Jan 80
# 587 - Nov 18
# 600 - Dec 19






data_ex_hkm=data[1:467,]-(rf[1:467]-1)%*%t(rep(1,ncol(data)))
HKM_alined=HKM[121:587,]

# on time varying number of factors
# 1 if dormient factors, then no glueing
# 2 if full time serie,s then glueing - use the projection interpretation
# my data Jan 80 to Dec 2019
factor=array(NA,c(window,10,nw))
R2=matrix(NA,nw,1)
R2x=matrix(NA,nw,1)
R2x1=matrix(NA,nw,1)
Ftest=matrix(NA,nw,1)
Ftestpval=matrix(NA,nw,1)
R2vec= matrix(NA,nw,10)
beta2=matrix(NA,nw,11)


factorb=array(NA,c(window,10,nw))
R2b=matrix(NA,nw,1)
R2xb=matrix(NA,nw,1)
R2x1b=matrix(NA,nw,1)
Ftestb=matrix(NA,nw,1)
Ftestpvalb=matrix(NA,nw,1)
R2vecb= matrix(NA,nw,10)
beta2b=matrix(NA,nw,11)

riskpremia.supp=matrix(NA,10,nw)

i=1
  for (i in 1:(nw-window))
  {
    print(i)
index = i:(i + window-1)
rf.supp = rf[index] - 1

# unbalanced
X=data_ex_hkm[index,]#-rf.supp%*%t(rep(1,ncol(data))) # i : i+w-1
delete = apply(X, 2, function(x){sum(is.na(x))})
delete = which(delete > 0)
N=ncol(X[,-delete])
# this is from i to i+windows-1 so size window
X = X[,-delete]


# balanced

delete_bal = apply(data_ex_hkm, 2, function(x){sum(is.na(x))})
delete_bal = which(delete_bal > 0)
N_bal=ncol(data_ex_hkm[,-delete_bal])
X_bal_all=data_ex_hkm[,-delete_bal]


X_bal=data_ex_hkm[index,-delete_bal]#-rf.supp%*%t(rep(1,N_bal)) # i : i+w-1


# UNBALANCED ANALYSIS

factor[,,i] = sqrt(window) * as.matrix(eigen(X %*% t(X))$vectors[,1:10])
factor[,,i] = factor[,,i] * (rep(1, window) %*% t(sign(apply(factor[,,i], 2, mean))))
riskpremia.supp[,i]=apply(factor[,,i],2,mean)


#  this is TS R2
reg=lm(HKM_alined[index,3]~factor[,,i])
R2[i]=var(reg$fitted.values)/var(HKM_alined[index,3])
beta2[i,]=reg$coefficients
R2vec[i,]=(diag(reg$coefficients[2:11]^2))%*%matrix(diag(var(factor[,,i])),10,1)/var(HKM_alined[index,3])



# this is XS R2

reg=lm(X~factor[,,i])
X_mean=apply(X,2,mean)
regxs=lm(X_mean~t(reg$coefficients[2:11,]))
R2x[i]=var(regxs$fitted.values)/var(X_mean)

facplushkm=cbind(factor[,,i],HKM_alined[index,3])
reg1=lm(X~facplushkm)
regxs1=lm(X_mean~t(reg1$coefficients[2:12,]))
R2x1[i]=var(regxs1$fitted.values)/var(X_mean)

Ftest[i]=(R2x1[i]-R2x[i])/((1-R2x1[i])/(N-12)  )
Ftestpval[i]=1-pchisq(Ftest[i],1)







# balanced

factorb[,,i] = sqrt(window) * as.matrix(eigen(X_bal %*% t(X_bal))$vectors[,1:10])
factorb[,,i] = factorb[,,i] * (rep(1, window) %*% t(sign(apply(factorb[,,i], 2, mean))))


#  this is TS R2
regb=lm(HKM_alined[index,3]~factorb[,,i])
R2b[i]=var(regb$fitted.values)/var(HKM_alined[index,3])
beta2b[i,]=regb$coefficients
R2vecb[i,]=((diag(regb$coefficients[2:11]^2))%*%matrix(diag(var(factorb[,,i])),10,1)/var(HKM_alined[index,3]))



# this is XS R2

reg=lm(X_bal~factorb[,,i])
X_meanb=apply(X_bal,2,mean)
regxs=lm(X_meanb~t(reg$coefficients[2:11,]))
R2xb[i]=var(regxs$fitted.values)/var(X_meanb)

facplushkmb=cbind(factorb[,,i],HKM_alined[index,3])
reg1=lm(X_bal~facplushkmb)
regxs1b=lm(X_meanb~t(reg1$coefficients[2:12,]))
R2x1b[i]=var(regxs1b$fitted.values)/var(X_meanb)

Ftestb[i]=(R2x1b[i]-R2xb[i])/((1-R2x1b[i])/(N-12)  )
Ftestpvalb[i]=1-pchisq(Ftestb[i],1)



  }

pdf('HKM_R2_unbalanced_17Jan23.pdf')
plot(x=ts.enddate[1:(nw-window)],type="l", y = R2[1:(nw-window)],
  main="Locally-Strong Factors: Local PCA (unb) vs Intermediary Capital  (T=24)",ylim=c(0,1),
     xlab="Date",ylab="R-square",col = 1)#,ylim=c(-0.002,0.003)) # Plot 1st histogram using a transparent color
lines(ts.enddate[1:(nw-window)], type="l", y = R2vec[1:(nw-window),1],
    col = 3, add = TRUE , freq=FALSE) # Add 2nd histogram using different color
lines(x = ts.enddate[1:(nw-window)], y = R2vec[1:(nw-window),2],  col = 4, add = TRUE , freq=FALSE) # Add 2nd histogram using different color
abline(h=R2u,col=2,lwd=3)
abline(v = rec[which(rec > 0)], col = "gray90")
legend(ts.enddate[1],0.4,c("total Rsquare", "Rsquare(1)", "Rsquare(2)", "constant Rsquare"), lwd=c(5,5), col=c(1,3,4,2), y.intersp=0.75)
dev.off()


pdf('HKM_R2_balanced_17Jan23.pdf')
plot(x=ts.enddate[1:(nw-window)],type="l", y = R2b[1:(nw-window)],
     main="Locally-Strong Factors: Local PCA (bal) vs Intermediary Capital  (T=24)",ylim=c(0,1),
     xlab="Date",ylab="R-square",col = 1)#,ylim=c(-0.002,0.003)) # Plot 1st histogram using a transparent color
lines(ts.enddate[1:(nw-window)], type="l", y = R2vecb[1:(nw-window),1],
      col = 3, add = TRUE , freq=FALSE) # Add 2nd histogram using different color
lines(x = ts.enddate[1:(nw-window)], y = R2vecb[1:(nw-window),4],  col = 4, add = TRUE , freq=FALSE) # Add 2nd histogram using different color
abline(h=R2u,col=2,lwd=3)
abline(v = rec[which(rec > 0)], col = "gray90")
legend(ts.enddate[1],0.4,c("total Rsquare", "Rsquare(1)", "Rsquare(4)", "constant Rsquare"), lwd=c(5,5), col=c(1,3,4,2), y.intersp=0.75)
dev.off()



# constant bal 



reg=lm(HKM_alined[1:467,3]~ts.factorsu[1:467,,1])
R2u=var(reg$fitted.values)/var(HKM_alined[1:467,3])
beta2u=reg$coefficients
R2vecu=(diag(reg$coefficients[2:11]^2))%*%matrix(diag(var(ts.factorsu[,,1])),10,1)/var(HKM_alined[1:467,3])



# this is XS R2

reg=lm(X_balanced[1:467,]~ts.factorsu[1:467,,1])
regxsu=lm(X_balanced_mean~t(reg$coefficients[2:11,]))
  R2xu=var(regxsu$fitted.values)/var(X_balanced_mean)

facplushkmu=cbind(ts.factorsu[1:467,,1],HKM[1:467,3])
reg1=lm(X_balanced[1:467,]~facplushkmu)
regxs1u=lm(X_balanced_mean~t(reg1$coefficients[2:12,]))
R2x1u=var(regxs1u$fitted.values)/var(X_balanced_mean)

Ftestu=(R2x1u-R2xu)/((1-R2x1u)/(N-12)  )
Ftestpvalu=1-pchisq(Ftestu,1)




# market return analyis

# UNBALANCED ANALYSIS  -  BALANCED ANALYSIS 

FFy = read.csv("F-F_Research_Data_Factors_yearly.csv", header = T)
attach(FFy)
names(FFy)


FFm = read.csv("F-F_Research_Data_Factors_monthly.csv", header = T)
attach(FFm)
names(FFm)
# from 643 to 1122

FFd = read.csv("F-F_Research_Data_Factors_daily.csv", header = T)
attach(FFd)
names(FFd)
reg=lm(Mkt.RF[(18604+20):(22651+20)]~IM[,4])
summary(reg)

Mkt.RFa=250*Mkt.RF
IMa=IM
IMa[,4:8]=100*IM[,4:8]



# FF are per cent but not annualized!

IM = read.csv("epbound.csv", header = T)
attach(IM)
names(IM)
View(IM)

100*apply(IM[,3:7],2,mean)#   IM is annualized but not  per cent
apply(FF,2,mean)


aggdata=aggregate(IM[,4:8],by=list(IM$year,IM$month),FUN=sum)
#a=aggdata[,1]==1997
t=1996
#newdata=matrix(NA,1,7)
rm(newdata)
for (t in 1996:2011)
{
       a=aggdata[,1]==t
if (t==1996)
{          newdata=   as.matrix(aggdata[a,],12,7) }
       else
    { 
      newdata=rbind(newdata,as.matrix(aggdata[a,],12,7))
      }
  
}
newdata[,3:7]=(12/250)*newdata[,3:7]# annualized
newdata[,3:7]=100*newdata[,3:7]# per cent

FFa=FF*12

# newdata from jan 96 to dec 11
# FF from  jan 80  to dec 19 -  here 170+23=193 is jan 96 - 361+23=384
# newdata: 4 is 1m, 5 is 2m, 6 is 3m, 7 is 6m, 8 is 12m



# PREPARATION DATA
r1=matrix(NA,nw,1)
r2=matrix(NA,nw,1)
r3=matrix(NA,nw,1)
riskpremia.supp=matrix(NA,nw,1)
r4=matrix(NA,nw,1)
r5=matrix(NA,nw,1)

  t=1
   for (t in 1:(nw))
    {
    r1[t]=mean(FF[t:(t+window-1),1]) # FF
    r2[t]=mean(ts.factors[,1,t]) #  local PCA
    r3[t]=mean(ts.factorsu[t:(t+window-1),1,1]) # constant PCA
    
  }


# glued factors
factorsTrot_Markus=matrix(NA,nw,10)
factorT=matrix(NA,nw,10)


i=1
for (i in 1:(nw-window-1))
{
  index = i:(i + window-1) 
  factor.supp=ts.factors[,,i]
  factorT[i+window-1,]=ts.factors[window,,i]
  
  supp=svd((t(factor.supp)%*%ts.factorsu[index,,1]), nu =10, nv = 10)#, LINPACK = FALSE)
  HHM=supp[[2]]%*%t(supp[[3]])
  factor.supp_rot_Markus=factor.supp%*%(HHM)
  factorsTrot_Markus[i+window-1,]=factor.supp_rot_Markus[window,]
}

cor(factorT[window:455,1],factorsTrot_Markus[window:455,1])

# timing
# newdata starts from Jan 1996 but different horizon
# average returns over T=24 so ts.enddate[193]=dec 197.

# 161 is Jan 1996  
plot(riskpremia.supp[(193):(193+191)])
plot((riskpremia.supp[(193+tt):(193+191+tt)]),newdata[,3])
RR2=matrix(NA,3*window,1)
tt=-2*window+1
for (tt in (-(2*window)+1):window)
  
    { reg=lm((r5[(193+tt):(193+191+tt)])~newdata[,3])
  
      RR2[(tt+2*window)]=var(reg$fitted.values)/var(r5[(193+tt):(193+191+tt)])
    
  }


# for localPCA
reg=lm((riskpremia.supp[(193+4):(193+191+4)])~newdata[,3])
var(reg$fitted.values)/var(riskpremia.supp[(193+4):(193+191+4)])
sqrt(var(reg$fitted.values)/var(riskpremia.supp[(193+4):(193+191+4)]))
reg$coefficients
plot((riskpremia.supp[(193+4):(193+191+4)]),newdata[,3])
cor((riskpremia.supp[(193+4):(193+191+4)]),newdata[,3])
plot((riskpremia.supp[(193+4):(193+191+4)]),type="l")
plot(newdata[,3],col=2)


# now the rescaled version of r2
cor((r2s[(193+4):(193+191+4)]),newdata[,3])




# for rolling FF 
reg=lm((r1[(193+3):(193+191+3)])~newdata[,3])
var(reg$fitted.values)/var(r1[(193+3):(193+191+3)])
sqrt(var(reg$fitted.values)/var(r1[(193+3):(193+191+3)]))
reg$coefficients
plot((r1[(193+3):(193+191+3)]),newdata[,3])
cor((r1[(193+3):(193+191+3)]),newdata[,3])


# for uncond PCA
reg=lm((r3[(193+3):(193+191+3)])~newdata[,3])
var(reg$fitted.values)/var(r3[(193+3):(193+191+3)])
sqrt(var(reg$fitted.values)/var(r3[(193+3):(193+191+3)]))
reg$coefficients
plot((r3[(193+3):(193+191+3)]),newdata[,3])
cor((r3[(193+3):(193+191+3)]),newdata[,3])
cor((r3[(193-22):(193+191-22)]),newdata[,3])

# for local PCA glued
reg=lm((r5[(193+4):(193+191+4)])~newdata[,3])
var(reg$fitted.values)/var(r5[(193+4):(193+191+4)])
sqrt(var(reg$fitted.values)/var(r5[(193+4):(193+191+4)]))
reg$coefficients
cor((r5[(193+4):(193+191+4)]),newdata[,3])
plot((r5[(193+4):(193+191+4)]),newdata[,3])
}

# e histograms etc to check units

# local FF

# r1 = rolling FF
plot((r1[(193+3):(193+191+3)]),newdata[,3])
cor((r1[(193+3):(193+191+3)]),newdata[,3])
plot((12*r1[(193+3):(193+191+3)]),type="l")

plot(x=ts.enddate[(193+3):(193+191+3)],y=12*r1[(193+3):(193+191+3)]) # Add 2nd histogram using different color
points(x=ts.enddate[(193+3):(193+191+3)],newdata[,3],col=2)
abline(v = rec[which(rec > 0)], col = "grey80")



12*mean(r1[(193+3):(193+191+3)])
mean(newdata[,3])

# hist
r1h <- hist(12*r1[(193+3):(193+191+3)], breaks = 30 , plot = FALSE)
imh <- hist(newdata[,3], breaks = 30 , plot = FALSE)

c1 <- rgb(173,216,230, max = 255, alpha = 120, names = "lt.blue")
c2 <- rgb(255,192,203, max = 255, alpha = 30, names = "lt.pink")
c3 <- rgb(144,238,144, max = 255, alpha = 30, names = "lt.green")

plot(r1h,   ,xlim=c(-50,50),
     xlab="Monthly Returns",col = c1) # Plot 1st histogram using a transparent color
plot(imh,  col = 2, add = TRUE) # Add 2nd histogram using different color

r1s=matrix(NA,nw,1)
r1s[(193+4):(193+191+4)]=mean(newdata[,3])+sd(newdata[,3])*(r1[(193+4):(193+191+4)]- mean(r1[(193+4):(193+191+4)]))/sd(r1[(193+4):(193+191+4)])
mean(r1s[(193+4):(193+191+4)])
r1h <- hist(12*r1[(193+4):(193+191+4)], breaks = 30 , plot = FALSE)
imh <- hist(newdata[,3], breaks = 30 , plot = FALSE)
mean(12*r1[(193+4):(193+191+4)])

# construct new colors - alpha gives the strength of the color
c1 <- rgb(173,216,230, max = 255, alpha = 120, names = "lt.blue")
c2 <- rgb(255,192,203, max = 255, alpha = 30, names = "lt.pink")
c3 <- rgb(144,238,144, max = 255, alpha = 30, names = "lt.green")
 c4 <- rgb(1,1,255, max = 255, alpha = 80, names = "lt.bo")
plot(r1h ,xlim=c(-45,45),
     xlab="Monthly Returns",col = c1) # Plot 1st histogram using a transparent color
#curve(dnorm(x, mean=mean(mfunds$drefus), sd=sd(mfunds$drefus)), 
#      col="darkblue", lwd=2, add=TRUE, yaxt="n")
plot(imh,  col = 2, add = TRUE) # Add 2nd histogram using different color


pdf('MRP_FF_PLAIN_LOCAL_17Jan23.pdf')
plot(r2sh,  main="Expected Market Return: \n Local PCA (rescaled) vs Market Factor " ,xlim=c(-45,45),ylim=c(0,0.2),
     xlab="Monthly Returns",col = c1, freq=F) # Plot 1st histogram using a transparent color
plot(r1h,  col = c4, add = TRUE, freq=F) # Add 2nd histogram using different color
legend(-10,0.15,c("Local PCA (resc)", "Market Factor"), lwd=c(5,5), col=c(c1,c4), y.intersp=0.75)
dev.off()


pdf('MRP_FF_PLAIN_17Jan23.pdf')
plot(r1h,  main="Expected Market Return: \n Market Factor  vs Option-Implied" ,xlim=c(-45,45),ylim=c(0,0.2),
     xlab="Monthly Returns",col = c1, freq=F) # Plot 1st histogram using a transparent color
plot(imh,  col = c3, add = TRUE, freq=F) # Add 2nd histogram using different color
legend(-10,0.15,c("Market Factor", "Option-Implied"), lwd=c(5,5), col=c(c1,c4), y.intersp=0.75)
dev.off()


r1s=matrix(NA,nw,1)
r1s[(193+4):(193+191+4)]=mean(newdata[,3])+sd(newdata[,3])*(r1[(193+4):(193+191+4)]- mean(r1[(193+4):(193+191+4)]))/sd(r1[(193+4):(193+191+4)])
mean(r1s[(193+4):(193+191+4)])
r1hs <- hist(r1s[(193+4):(193+191+4)], breaks = 30 , plot = FALSE)


pdf('MRP_FF_17Jan23.pdf')
plot(r1hs,  main="Expected Market Return: \n Market Factor  vs Option-Implied" ,xlim=c(-15,15),ylim=c(0,0.2),
     xlab="Monthly Returns",col = c1, freq=F) # Plot 1st histogram using a transparent color
plot(imh,  col = c3, add = TRUE, freq=F) # Add 2nd histogram using different color
legend(-10,0.15,c("Market Factor", "Option-Implied"), lwd=c(5,5), col=c(c1,c4), y.intersp=0.75)
dev.off()




# same mean
12*mean(r2[(193+4):(193+191+4)])
mean(newdata[,3])

# hist
r2h <- hist(12*r2[(193+4):(193+191+4)], breaks = 30 , plot = FALSE)
imh <- hist(newdata[,3], breaks = 30 , plot = FALSE)

# construct new colors - alpha gives the strength of the color
c1 <- rgb(173,216,230, max = 255, alpha = 120, names = "lt.blue")
c2 <- rgb(255,192,203, max = 255, alpha = 30, names = "lt.pink")
c3 <- rgb(144,238,144, max = 255, alpha = 30, names = "lt.green")
 c4 <- rgb(1,1,255, max = 255, alpha = 80, names = "lt.bo")

 plot(r2h, xlim=c(-15,15),
     xlab="Monthly Returns",col = c1) # Plot 1st histogram using a transparent color
plot(imh,  col = c4, add = TRUE) # Add 2nd histogram using different color


pdf('MRP_LOCAL_PLAIN_17Jan23.pdf')
plot(r2h,  main="Expected Market Return: \n Local PCA  vs Option-Implied" ,xlim=c(-15,15),ylim=c(0,0.3),
     xlab="Monthly Returns",col = c1, freq=F) # Plot 1st histogram using a transparent color
plot(imh,  col = c4, add = TRUE, freq=F) # Add 2nd histogram using different color
legend(-15,0.15,c("local PCA", "Option-Implied"), lwd=c(5,5), col=c(c1,c4), y.intersp=0.75)
dev.off()

# now make units of r2 as IM (fix rotation): important

r2s=matrix(NA,nw,1)
r2s=mean(newdata[,3])+sd(newdata[,3])*(r2- mean(r2))/sd(r2)
mean(r2s)


mean(r2s[(193+4):(193+191+4)])

r2sh <- hist(r2s, breaks = 30 ,  freq=FALSE)#plot = FALSE ,
plot(r2sh,  main="HISTOGRAM and PDF: \nDREFUS PORTFOLIO RETURN" ,xlim=c(-20,20),
     xlab="Monthly Returns",col = c1, freq=F) # Plot 1st histogram using a transparent color
plot(imh,  col = 2, add = TRUE, freq=F) # Add 2nd histogram using different color


pdf('MRP_LOCAL_17Jan23.pdf')
plot(r2sh,  main="Expected Market Return: \n Local PCA (rescaled)  vs Option-Implied" ,xlim=c(-15,15),ylim=c(0,0.2),
     xlab="Monthly Returns",col = c1, freq=F) # Plot 1st histogram using a transparent color
plot(imh,  col = c4, add = TRUE, freq=F) # Add 2nd histogram using different color
legend(-15,0.15,c("local PCA (resc)", "Option-Implied"), lwd=c(5,5), col=c(c1,c4), y.intersp=0.75)
dev.off()



plot(r2sh,  main="Expected Market Return: \n Local PCA (rescaled)  vs Option-Implied" ,xlim=c(-15,15),ylim=c(0,0.2),
     xlab="Monthly Returns",col = c1, freq=F) # Plot 1st histogram using a transparent color

pdf('MRP_LOCAL_MKT_MARTIN_timeseries_17Jan23.pdf')
plot(x=ts.enddate[(193+3):(193+191+3)],y=r2s[(193+3):(193+191+3)],type="l",ylim=c(-30,30),
     main="Expected Market Return: \n Local PCA (rescaled)  vs Option-Implied vs Market Factor" ,
     xlab="Monthly Returns",ylab="date"
     ) # Add 2nd histogram using different color
lines(x=ts.enddate[(193+3):(193+191+3)],newdata[,3],col=2)
lines(x=ts.enddate[(193+3):(193+191+3)],y=12*r1[(193+3):(193+191+3)],col=3) # Add 2nd histogram using different color
abline(v = rec[which(rec > 0)], col = "grey80")
legend(ts.enddate[(193+3)],-20,c("local PCA (resc)", "Option-Implied","Market Factor"), lwd=c(5,5), col=c(1,2,3), y.intersp=0.75)
dev.off() 



# r3 = unco PCA
plot((r3[(193+3):(193+191+3)]),newdata[,3])
cor((r3[(193+3):(193+191+3)]),newdata[,3])

# same mean 
12*mean(r3[(193+3):(193+191+3)])
mean(newdata[,3])

# hist
r3h <- hist(12*r3[(193+3):(193+191+3)], breaks = 30 , plot = FALSE)
imh <- hist(newdata[,3], breaks = 30 , plot = FALSE)

# construct new colors - alpha gives the strength of the color
c1 <- rgb(173,216,230, max = 255, alpha = 120, names = "lt.blue")
c2 <- rgb(255,192,203, max = 255, alpha = 30, names = "lt.pink")
c3 <- rgb(144,238,144, max = 255, alpha = 30, names = "lt.green")

plot(r3h,  xlim=c(-15,15),
     xlab="Monthly Returns",col = c1) # Plot 1st histogram using a transparent color
plot(imh,  col = c2, add = TRUE) # Add 2nd histogram using different color



# now make units of r3 as IM (fix rotation): important

r3s=mean(newdata[,3])+sd(newdata[,3])*(r3[(193+4):(193+191+4)]- mean(r3[(193+4):(193+191+4)]))/sd(r3[(193+4):(193+191+4)])
#mean(r2s)
r3sh <- hist(r3s, breaks = 30 ,  freq=FALSE)#plot = FALSE ,
plot(r3sh,  xlim=c(-20,20),
     xlab="Monthly Returns",col = c1, freq=F) # Plot 1st histogram using a transparent color
plot(imh,  col = 2, add = TRUE, freq=F) # Add 2nd histogram using different color


pdf('MRP_CONSTANT_17Jan23.pdf')
plot(r3sh,  main="Expected Market Return: \n Constant PCA vs Option-Implied" ,xlim=c(-20,20),ylim=c(0,0.2),
     xlab="Monthly Returns",col = c1, freq=F) # Plot 1st histogram using a transparent color
plot(imh,  col = c4, add = TRUE, freq=F) # Add 2nd histogram using different color
legend(-15,0.15,c("Constant PCA", "Option-Implied"), lwd=c(5,5), col=c(c1,c4), y.intersp=0.75)
dev.off()



# same mean
12*mean(r5[(193+4):(193+191+4)])
mean(newdata[,3])

# hist
r5h <- hist(12*r5[(193+3):(193+191+3)], breaks = 30 , plot = FALSE)
imh <- hist(newdata[,3], breaks = 30 , plot = FALSE)
# construct new colors - alpha gives the strength of the color
c1 <- rgb(173,216,230, max = 255, alpha = 120, names = "lt.blue")
c2 <- rgb(255,192,203, max = 255, alpha = 30, names = "lt.pink")
c3 <- rgb(144,238,144, max = 255, alpha = 30, names = "lt.green")
# c4 <- rgb(1,1,255, max = 255, alpha = 80, names = "lt.bo")
plot(r5h,  main="HISTOGRAM and PDF: \nDREFUS PORTFOLIO RETURN" ,xlim=c(-15,15),
     xlab="Monthly Returns",col = c1) # Plot 1st histogram using a transparent color
plot(imh,  col = c2, add = TRUE) # Add 2nd histogram using different color

# now make units of r5 as IM (fix rotation): important

r5s=mean(newdata[,3])+sd(newdata[,3])*(r5[(193+4):(193+191+4)]- mean(r5[(193+4):(193+191+4)]))/sd(r5[(193+4):(193+191+4)])
mean(r5s)
r5sh <- hist(r5s, breaks = 30 ,  freq=FALSE)#plot = FALSE ,


pdf('MRP_GLUED_17Jan23.pdf')
plot(r5sh,  main="Expected Market Return: \n Local PCA (glued) vs Option-Implied" ,xlim=c(-15,15),ylim=c(0,0.2),
     xlab="Monthly Returns",col = c1, freq=F) # Plot 1st histogram using a transparent color
plot(imh,  col = c4, add = TRUE, freq=F) # Add 2nd histogram using different color
legend(-15,0.15,c("local PCA (glued)", "Option-Implied"), lwd=c(5,5), col=c(c1,c4), y.intersp=0.75)
dev.off()









# R2


r2
r2u
r2FF
r2ufull
r2FFfull


pdf('T120_R2_17Jan23.pdf')
plot(x=ts.enddate[241:TTT],y=gc[241:TTT,1],type="l",ylim=c(0,1),
     main="Canonical Correlations: Factors \n local PCA vs constant PCA (T=120)" ,
     xlab="Date",ylab="Canonical Correlation"
) # Add 2nd histogram using different color
lines(x=ts.enddate[241:TTT],y=gc[241:TTT,2],col=2)
lines(x=ts.enddate[241:TTT],y=gc[241:TTT,3],col=3)
lines(x=ts.enddate[241:TTT],y=gc[241:TTT,4],col=4)
lines(x=ts.enddate[241:TTT],y=gc[241:TTT,5],col=5)
abline(v = rec[which(rec > 0)], col = "grey80")
legend(ts.enddate[241],0.6,c("gc=1", "gc=2","gc=3","gc=4","gc=5","econ-fin crises"), lwd=c(5,5), col=c(1,2,3,4,5,"grey"), y.intersp=0.75)
dev.off()


# /Users/pz/Dropbox/factors under HJ distance/Bai e Ng - Simulations/04_Empirics/T24oos_31Jan2023.RData
#T=24

# SDF and business cycle

library(foreign)
data_nt= read.csv("Dataset_NonTraded.csv", stringsAsFactors = T)
data_t= read.csv("Dataset_Traded.csv", stringsAsFactors = T)
attach(data_t)
attach(data_nt)
# 94 =292
#476 = 674
# 1 = 199

# regressions
nber=1*(rec>0)
dim(nber)
reg=lm(sdfT[2:457]~nber[2:457])
summary(reg)

reg=lm(sdfT[2:457]~rec[2:457])
summary(reg)



reg=lm(sdfT_vec[10,2:457]~nber[2:457])
summary(reg)

reg=lm(sdfu[25:480,10]~nber[2:457])
summary(reg)



#credit spread
#BB BC 54-55

{
  #476 =(453 in T) = 674
##  480 = (457 in T) = 678

# 1 = 199
# 24 = (1 in T) = 222
}

reg=lm(sdfT[2:453]~data_nt[223:674,55])
summary(reg)

reg=lm(sdfTFF[2:453]~data_nt[223:674,55])
summary(reg)


reg=lm(sdfT_vec[1,2:453]~data_nt[223:674,55])
summary(reg)

reg=lm(sdfu[25:476,10]~data_nt[223:674,55])
summary(reg)

  reg=lm(sdfuFF[25:476,10]~data_nt[223:674,55])
summary(reg)

# fin uncer
#BE-BG - 57-59

reg=lm(sdfT[2:453]~data_nt[223:674,59])
summary(reg)
reg=lm(sdfTFF[2:453]~data_nt[223:674,59])
summary(reg)

reg=lm(sdfT_vec[10,2:453]~data_nt[223:674,57])
summary(reg)

reg=lm(sdfu[25:476,10]~data_nt[223:674,59])
summary(reg)

reg=lm(sdfuFF[25:476,10]~data_nt[223:674,59])
summary(reg)

#TED
#AY 51

reg=lm(sdfT[2:453]~data_nt[223:674,51])
summary(reg)

reg=lm(sdfTFF[2:453]~data_nt[223:674,51])
summary(reg)

reg=lm(sdfT_vec[1,2:453]~data_nt[223:674,51])
summary(reg)

reg=lm(sdfu[25:476,10]~data_nt[223:674,51])
summary(reg)

reg=lm(sdfuFF[25:476,10]~data_nt[223:674,51])
summary(reg)


#VIX
#AJ-AL  36-38

reg=lm(sdfT[2:453]~data_nt[223:674,37])
summary(reg)

reg=lm(sdfTFF[2:453]~data_nt[223:674,38])
summary(reg)



  reg=lm(sdfT_vec[4,2:453]~data_nt[223:674,37])
  summary(reg)



  reg=lm(sdfu[25:476,10]~data_nt[223:674,38])
  summary(reg)

  reg=lm(sdfuFF[25:476,10]~data_nt[223:674,38])
  summary(reg)
  
# div yield
#CM-CN 91-92

  reg=lm(sdfT[2:453]~data_nt[223:674,92])
  summary(reg)
  reg=lm(sdfTFF[2:453]~data_nt[223:674,92])
  summary(reg)
  
  reg=lm(sdfT_vec[10,2:453]~data_nt[223:674,92])
  summary(reg)
  
  reg=lm(sdfu[25:476,10]~data_nt[223:674,92])
  summary(reg)
  
  reg=lm(sdfuFF[25:476,10]~data_nt[223:674,92])
  summary(reg)
  
  
  # agg liq
#CY-CZ 103-104
  reg=lm(sdfT[2:453]~data_nt[223:674,104])
  summary(reg)
  
  reg=lm(sdfTFF[2:453]~data_nt[223:674,104])
  summary(reg)
  
  reg=lm(sdfT_vec[10,2:453]~data_nt[223:674,103])
  summary(reg)
  
  reg=lm(sdfu[25:476,10]~data_nt[223:674,104])
  summary(reg)
  reg=lm(sdfuFF[25:476,10]~data_nt[223:674,104])
  summary(reg)
  

# int const (kelly)
#CE-CF 83-84


  reg=lm(sdfT[2:453]~data_nt[223:674,84])
  summary(reg)
  reg=lm(sdfTFF[2:453]~data_nt[223:674,84])
  summary(reg)
  
  
  reg=lm(sdfT_vec[1,2:453]~data_nt[223:674,84])
  summary(reg)
  
  reg=lm(sdfu[25:476,10]~data_nt[223:674,84])
  summary(reg)
  
  cor(sdfu[25:476,10],sdfT_vec[10,2:453])

  #T=24
  
  #  regresssion SDF on state variables bs all together
  y = sdfT[2:453]
  Dataset_full=  data_nt[223:674,]
  View(data_nt)
  
  
  temp = lm(y~
              TEDSpread[223:674]+
            BCI[223:674]+
            MDI[223:674]+
            REC[223:674]+
            UNRATE_FOD[223:674]+
            Lu_Ng_f2[223:674]+
            TERM[223:674]+
            FRED_MACRO_PCA5[223:674]+
            MACRO_UNC_AR1_12month[223:674]+
            BW_INV_SENT_FOD[223:674]+
            CEA_FOD[223:674]+
            VIX_CLOSE_FOD[223:674]+
            DELTA_SLOPE[223:674]+
            FinCond[223:674]+
            MACRO_UNC_12month[223:674]+
            FRED_MACRO_PCA8[223:674]+
            CREDIT_FOD[223:674]+
            MACRO_UNC_AR1_3month[223:674]+
            IND_PROD_FOD[223:674],na.action = na.exclude)
              
  temp = lm(y~as.matrix(data_nt$REC[223:674]), na.action = na.exclude)
  summary(temp)
  temp = lm(sdfT[2:457]~nber[2:457], na.action = na.exclude)
  summary(temp)
  
  
  
  R2 = corr = pv  = tstat = rep(NA, ncol(Dataset_full))
  for (j in 1:length(R2))
  {
    x = Dataset_full[,j]
    corr[j] = cor(y, x, use = "complete.obs")
    temp = lm(y ~ x, na.action = na.exclude)
    pv[j] = summary(temp)[[4]][2,4]
    tstat[j] = summary(temp)[[4]][2,3]
    R2[j] = summary(temp)[[8]]
  } 
  
# uncond
  
  y = sdfu[25:476,10]
  Dataset_full=  data_nt[223:674,]
  
  R2u = corru = pvu  = tstatu = rep(NA, ncol(Dataset_full))
  for (j in 1:length(R2u))
  {
    x = Dataset_full[,j]
    corru[j] = cor(y, x, use = "complete.obs")
    temp = lm(y ~ x, na.action = na.exclude)
    pvu[j] = summary(temp)[[4]][2,4]
    tstatu[j] = summary(temp)[[4]][2,3]
    R2u[j] = summary(temp)[[8]]
  } 
  
  
  
 #   library(plyr)
#  NT=cbind(names(data_nt),round(pv,digits=4),round(R2,digits=4),round(tstat,digits=4),round(pvu,digits=4),round(R2u,digits=4),round(tstatu,digits=4)    )
#  write.csv(NT,"NT.csv", row.names = FALSE)
  
  #T=120
  

  y = sdfT[2:361]
  Dataset_full=  data_nt[319:678,]
  View(data_nt)
  
  
  temp = lm(y ~as.matrix(data_nt[223:674,]), na.action = na.exclude)
  summary(temp)
  
  
  R2 = corr = pv  = tstat = rep(NA, ncol(Dataset_full))
  for (j in 1:length(R2))
  {
    x = Dataset_full[,j]
    corr[j] = cor(y, x, use = "complete.obs")
    temp = lm(y ~ x, na.action = na.exclude)
    pv[j] = summary(temp)[[4]][2,4]
    tstat[j] = summary(temp)[[4]][2,3]
    R2[j] = summary(temp)[[8]]
  } 
  
  # uncond
  
  y = sdfu[25:476,10]
  Dataset_full=  data_nt[223:674,]
  
  R2u = corru = pvu  = tstatu = rep(NA, ncol(Dataset_full))
  for (j in 1:length(R2u))
  {
    x = Dataset_full[,j]
    corru[j] = cor(y, x, use = "complete.obs")
    temp = lm(y ~ x, na.action = na.exclude)
    pvu[j] = summary(temp)[[4]][2,4]
    tstatu[j] = summary(temp)[[4]][2,3]
    R2u[j] = summary(temp)[[8]]
  } 
  
  
  
 # library(plyr)
  #round_any(as.matrix(cbind(names(data_nt),pv,R2,tstat)),accuracy=.0001, f=floor)
#  NT=cbind(names(data_nt),round(pv,digits=4),round(R2,digits=4),round(tstat,digits=4),round(pvu,digits=4),round(R2u,digits=4),round(tstatu,digits=4)    )
  
 # write.csv(NT,"NT120.csv", row.names = FALSE)
  
  
  

  
# Jan effect
  
  is.wholenumber <-
    function(x, tol = .Machine$double.eps^0.5)  abs(x - round(x)) < tol
  is.wholenumber(1) # is TRUE
  t=12
  dj=matrix(0,457,1)
  for (t in 12:457)
  {
    
    {  if(is.wholenumber(((t-2)/12))) dj[t]=1}
  

  }
  dj[2]=1
    


  reg=lm(sdfT[2:457]~dj[2:457])
  summary(reg)
  
  reg=lm(FF[24:480,1]~dj[1:457])
  summary(reg)
  
  
  
  reg=lm(data_t$MKT[223:678]~dj[2:457])
  summary(reg)
  
   

  reg=lm(sdfT_vec[2,2:457]~dj[2:457])
  summary(reg)
  
  reg=lm(sdfu[24:480,10]~dj[1:457])
  summary(reg)
  
  
 # market return prediction
  

#OOS R2
# no estimation case

eps_im1=FFa[(194+1):(194+191+1),1]-newdata[,3]
eps_mk =FFa[(194+1):(194+191+1),1]-r1[(194-window+1):(194-window+1+191)]
eps_lpca1 =FFa[(194+1):(194+191+1),1]-r2s[(194-window+1):(194-window+1+191)]




R2_im1=1-(t(eps_im1)%*%eps_im1)/(t(eps_mk)%*%eps_mk)
R2_lpca1=1-(t(eps_lpca1)%*%eps_lpca1)/(t(eps_mk)%*%eps_mk)

R2_im1
R2_lpca1



eps_im12=FFa[(194+12):(194+191+12),1]-newdata[,7]

eps_mk12 =FFa[(194+12):(194+191+12),1]-r1[(194-window+1):(194-window+1+191)]

eps_lpca12 =FFa[(194+12):(194+191+12),1]-r2s[(194-window+1):(194-window+1+191)]

R2_im12=1-(t(eps_im12)%*%eps_im12)/(t(eps_mk12)%*%eps_mk12)
R2_lpca12=1-(t(eps_lpca12)%*%eps_lpca12)/(t(eps_mk12)%*%eps_mk12)

R2_im12
R2_lpca12


#factorT and glued

eps_lpcaT1 =FFa[(194+1):(194+191+1),1]-factorT[(194-window+1):(194-window+1+191)]
eps_lpcaT12 =FFa[(194+12):(194+191+12),1]-factorT[(194-window+1):(194-window+1+191)]
eps_lpcarot1 =FFa[(194+1):(194+191+1),1]-factorsTrot_Markus[(194-window+1):(194-window+1+191)]
eps_lpcarot12 =FFa[(194+12):(194+191+12),1]-factorsTrot_Markus[(194-window+1):(194-window+1+191)]

R2_lpcaT1=1-(t(eps_lpcaT1)%*%eps_lpcaT1)/(t(eps_mk12)%*%eps_mk12)
R2_lpcaT12=1-(t(eps_lpcaT12)%*%eps_lpcaT12)/(t(eps_mk12)%*%eps_mk12)
R2_lpcarot1=1-(t(eps_lpcarot1)%*%eps_lpcarot1)/(t(eps_mk12)%*%eps_mk12)
R2_lpcarot12=1-(t(eps_lpcarot12)%*%eps_lpcarot12)/(t(eps_mk12)%*%eps_mk12)

R2_lpcaT1
R2_lpcaT12
R2_lpcarot1
R2_lpcarot12





# with estimation


reg=lm(FFa[(194-100+1):(194+1),1]~r2[(194-window+1-100):(194-window+1)])
eps_lpca11= FFa[(194+1):(194+191+1),1]- reg$coefficients[1]-
  reg$coefficients[2]*r2[(194-window+1):(194+191-window+1)]
R2_lpa11=1-(t(eps_lpca11)%*%eps_lpca11)/(t(eps_mk)%*%eps_mk)


reg=lm(FFa[(194-100+12):(194+12),1]~r2[(194-window+1-100):(194-window+1)])
eps_lpca112= FFa[(194+12):(194+191+12),1]- reg$coefficients[1]-
  reg$coefficients[2]*r2[(194-window+1):(194+191-window+1)]
R2_lpa112=1-(t(eps_lpca112)%*%eps_lpca112)/(t(eps_mk)%*%eps_mk)

R2_lpa11
R2_lpa112


reg=lm(FFa[(194-100+1):(194+1),1]~factorT[(194-100):(194)])
eps_lpca1T= FFa[(194+1):(194+191+1),1]- reg$coefficients[1]-
  reg$coefficients[2]*factorT[(194):(194+191)]
R2_lpa1T=1-(t(eps_lpca1T)%*%eps_lpca1T)/(t(eps_mk)%*%eps_mk)

reg=lm(FFa[(194-100+12):(194+12),1]~factorT[(194-100):(194)])
eps_lpca12T= FFa[(194+12):(194+191+12),1]- reg$coefficients[1]-
  reg$coefficients[2]*factorT[(194):(194+191)]
R2_lpa12T=1-(t(eps_lpca12T)%*%eps_lpca12T)/(t(eps_mk)%*%eps_mk)



R2_lpa1T
R2_lpa12T



reg=lm(FFa[(194-100+1):(194+1),1]~factorsTrot_Markus[(194-100):(194)])
eps_lpca1rot= FFa[(194+1):(194+191+1),1]- reg$coefficients[1]-
  reg$coefficients[2]*factorsTrot_Markus[(194):(194+191)]
R2_lpa1rot=1-(t(eps_lpca1rot)%*%eps_lpca1rot)/(t(eps_mk)%*%eps_mk)

reg=lm(FFa[(194-100+12):(194+12),1]~factorsTrot_Markus[(194-100):(194)])
eps_lpca12rot= FFa[(194+12):(194+191+12),1]- reg$coefficients[1]-
  reg$coefficients[2]*factorsTrot_Markus[(194):(194+191)]
R2_lpa12rot=1-(t(eps_lpca12rot)%*%eps_lpca12rot)/(t(eps_mk)%*%eps_mk)


R2_lpa1rot
R2_lpa12rot




reg=lm(FFa[(476-91-191-100+1):(476-91-191+1),1]~
         data_t$HML[(674-91-191-100):(674-91-191)])
eps_hml1=FFa[(476-91-191+1):(476-91+1),1] -reg$coefficients[1]-
  reg$coefficients[2]*data_t$HML[(674-91-191):(674-91)]
R2_hml1=1-(t(eps_hml1)%*%eps_hml1)/(t(eps_mk)%*%eps_mk)


reg=lm(FFa[(476-91-191-100+12):(476-91-191+12),1]~
         data_t$HML[(674-91-191-100):(674-91-191)])
eps_hml12=FFa[(476-91-191+12):(476-91+1),1] -reg$coefficients[1]-
  reg$coefficients[2]*data_t$HML[(674-91-191):(674-91)]
R2_hml12=1-(t(eps_hml12)%*%eps_hml12)/(t(eps_mk)%*%eps_mk)
  
  R2_hml1
  R2_hml12




reg=lm(FFa[(476-91-191+1):(476-91+1),1]~
         data_t$div12m_me[(674-91-191):(674-91)])
summary(reg)

# divp

reg=lm(FFa[(476-91-191-100+1):(476-91-191+1),1]~
         data_t$divp[(674-91-191-100):(674-91-191)])
eps_divp1=FFa[(476-91-191+1):(476-91+1),1] -reg$coefficients[1]-
  reg$coefficients[2]*data_t$divp[(674-91-191):(674-91)]
R2_divp1=1-(t(eps_divp1)%*%eps_divp1)/(t(eps_mk)%*%eps_mk)

reg=lm(FFa[(476-91-191-100+12):(476-91-191+12),1]~
         data_t$divp[(674-91-191-100):(674-91-191)])
eps_divp12=FFa[(476-91-191+12):(476-91+12),1] -reg$coefficients[1]-
  reg$coefficients[2]*data_t$divp[(674-91-191):(674-91)]
R2_divp12=1-(t(eps_divp12)%*%eps_divp12)/(t(eps_mk)%*%eps_mk)

R2_divp1
R2_divp12

# be_me


reg=lm(FFa[(476-91-191-100+1):(476-91-191+1),1]~
         data_t$be_me[(674-91-191-100):(674-91-191)])
eps_be1=FFa[(476-91-191+1):(476-91+1),1] -reg$coefficients[1]-
  reg$coefficients[2]*data_t$be_me[(674-91-191):(674-91)]
R2_be1=1-(t(eps_be1)%*%eps_be1)/(t(eps_mk)%*%eps_mk)

reg=lm(FFa[(476-91-191-100+12):(476-91-191+12),1]~
         data_t$be_me[(674-91-191-100):(674-91-191)])
eps_be12=FFa[(476-91-191+12):(476-91+12),1] -reg$coefficients[1]-
  reg$coefficients[2]*data_t$be_me[(674-91-191):(674-91)]
R2_be12=1-(t(eps_be12)%*%eps_be12)/(t(eps_mk)%*%eps_mk)

R2_be1
R2_be12




#  spectrum of SDF

ar1=matrix(0,(1*nw),1)
t=1
for (t in 2:(1*nw))
{          ar1[t]=ar1[t-1]*(0.99)+rnorm(1,0,1)}

plot(ar1)
ard <- spectrum(ar1)
plot(ard)

d <- spectrum(sdfT_vec[1,2:nw])
du <-  spectrum(sdfu_vec[2:nw,1,2])
dm <-  spectrum(FF[,1])
du1 <-  spectrum(ts.factorsu[,1,1])
dmk1 <-  spectrum( factorsTrot_Markus[25:nw,1])

xx=(1:nw)*(2*pi)/nw

dfft=    fft((sdfT_vec[1,2:nw]-mean(sdfT_vec[1,2:nw])), inverse = FALSE)
perd=Re(dfft*Conj(dfft)   ) /(2*pi*nw)
perd=perd/(sum(perd)/(2*pi))
plot(xx[2:((nw+1)/2)],perd[2:((nw+1)/2)],type="l",lwd=3)
x=xx[2:((nw+1)/2)]
y=perd[2:((nw+1)/2)]



dfft=    fft((sdfT_vec[1,2:nw]-mean(sdfT_vec[1,2:nw])), inverse = FALSE)
perd=Re(dfft*Conj(dfft)   ) /(2*pi*nw)
perd=perd/(sum(perd)/(2*pi))
plot(xx[2:((nw+1)/2)],perd[2:((nw+1)/2)],type="l",lwd=3)
x=xx[2:((nw+1)/2)]
y=perd[2:((nw+1)/2)]
nw_d=ksmooth(x,y, "normal", bandwidth = 0.2, range.x = range(x))

plot(xx[2:((nw+1)/2)],perd[2:((nw+1)/2)],type="l",lwd=3)
lines(nw_d$x,nw_d$y,col=2,lwd=3)



dfft=    fft((sdfu_vec[2:nw,10,2]-mean(sdfu_vec[2:nw,10,2])), inverse = FALSE)
perdu=Re(dfft*Conj(dfft)   ) /(2*pi*nw)
perdu=perdu/(sum(perdu)/(2*pi))
plot(xx[2:((nw+1)/2)],perd[2:((nw+1)/2)],type="l",lwd=3)
lines(xx[2:((nw+1)/2)],perdu[2:((nw+1)/2)],type="l",lwd=3,col=2)

x=xx[2:((nw+1)/2)]
y=perdu[2:((nw+1)/2)]

nw_du=ksmooth(x,y, "normal", bandwidth = 0.2, range.x = range(x))
plot(xx[2:((nw+1)/2)],perdu[2:((nw+1)/2)],type="l",lwd=3)
lines(nw_du$x,nw_du$y,col=2,lwd=3)
lines(nw_d$x,nw_d$y,col=3,lwd=3)





 

dfft=    fft((FF[,1]-mean(FF[,1])), inverse = FALSE)
perdFF=Re(dfft*Conj(dfft)   )/(2*pi*nw)
perdFF=perdFF/(sum(perdFF)/(2*pi))
plot(xx[2:((nw+1)/2)],perdFF[2:((nw+1)/2)],type="l",lwd=3)
x=xx[2:((nw+1)/2)]
y=perdFF[2:((nw+1)/2)]
data_ggp <- data.frame(x, y)


nw_dFF=ksmooth(x,y, "normal", bandwidth = 0.2, range.x = range(x))
plot(xx[2:((nw+1)/2)],perdFF[2:((nw+1)/2)],type="l",lwd=3)
lines(xx[2:((nw+1)/2)],perd[2:((nw+1)/2)],type="l",lwd=3,col=2)


plot(nw_d$x,nw_d$y,col=1,lwd=3,type="l",ylim=c(0.005,0.03))
lines(nw_du$x,nw_du$y,col=3,lwd=3)
lines(nw_dFF$x,nw_dFF$y,col=4,lwd=3)

# plots spectral analysis SDF


ggplot(data_ggp, aes(x, y)) +   # Draw ggplot2 scatterplot with smooth curve
  geom_point() +
  geom_smooth(se = FALSE,
              method = "loess",
              formula = y ~ x,span=0.3)

dfft=    fft(factorsTrot_Markus[25:nw,10], inverse = FALSE)
perdmark=Re(dfft*Conj(dfft)   )/(2*pi*nw) 

x=xx[2:((nw+1)/2)]
y=perdmark[2:((nw+1)/2)]

nw_d=ksmooth(x,y, "normal", bandwidth = 0.1, range.x = range(x))
plot(xx[2:((nw+1)/2)],perdmark[2:((nw+1)/2)],type="l",lwd=3)
lines(nw_d$x,nw_d$y,col=2,lwd=3)




ggplot(data_ggp, aes(x, y)) +   # Draw ggplot2 scatterplot with smooth curve
  geom_point() +
  geom_smooth(se = FALSE,
              method = "loess",
              formula = y ~ x,span=0.3)

zz=geom_smooth(se = FALSE,
               method = "loess",
               formula = y ~ x,span=0.3)

dfft=    fft(ar1, inverse = FALSE)
perd=Re(dfft*Conj(dfft)   )/(2*pi*(nw)) 
plot(perd[2:(nw)],type="l")



numf=1
xx=(1:nw)*(2*pi)/nw

dfft=    fft((sdfT_vec[numf,2:nw]-mean(sdfT_vec[numf,2:nw])), inverse = FALSE)
perd=Re(dfft*Conj(dfft)   ) /(2*pi*nw)
perd=perd/(sum(perd)/(2*pi))
x=xx[2:((nw+1)/2)]
y=perd[2:((nw+1)/2)]
nw_d=ksmooth(x,y, "normal", bandwidth = 0.2, range.x = range(x))

dfft=    fft((sdfu_vec[2:nw,numf,2]-mean(sdfu_vec[2:nw,numf,2])), inverse = FALSE)
perdu=Re(dfft*Conj(dfft)   ) /(2*pi*nw)
perdu=perdu/(sum(perdu)/(2*pi))
x=xx[2:((nw+1)/2)]
y=perdu[2:((nw+1)/2)]
nw_du=ksmooth(x,y, "normal", bandwidth = 0.2, range.x = range(x))

dfft=    fft((factorsTrot_Markus[25:nw,1]-mean(factorsTrot_Markus[25:nw,1])), inverse = FALSE)
perdum=Re(dfft*Conj(dfft)   ) /(2*pi*nw)
perdum=perdum/(sum(perdum)/(2*pi))
x=xx[2:((nw+1)/2)]
y=perdum[2:((nw+1)/2)]
nw_dm=ksmooth(x,y, "normal", bandwidth = 0.2, range.x = range(x))

# optimal k

dfft=    fft((sdfT[2:nw]-mean(sdfT[2:nw])), inverse = FALSE)
perd=Re(dfft*Conj(dfft)   ) /(2*pi*nw)
perd=perd/(sum(perd)/(2*pi))
x=xx[2:((nw+1)/2)]
y=perd[2:((nw+1)/2)]
nw_d=ksmooth(x,y, "normal", bandwidth = 0.2, range.x = range(x))

dfft=    fft((sdfu[2:nw,2]-mean(sdfu[2:nw,2])), inverse = FALSE)
perdu=Re(dfft*Conj(dfft)   ) /(2*pi*nw)
perdu=perdu/(sum(perdu)/(2*pi))
x=xx[2:((nw+1)/2)]
y=perdu[2:((nw+1)/2)]
nw_du=ksmooth(x,y, "normal", bandwidth = 0.2, range.x = range(x))





pdf('T24_SDF_spectral_K1_7Feb23.pdf')
plot(x=nw_d$x,y=nw_d$y,main="SDF: Spectral Analysis \n  local PCA  vs. constant PCA  (T=24,r=1)" ,
     xlab="frequency",ylab="SDF spectrum", type="l",col = 1,lwd=3,ylim=c(0.005,0.06)) # Plot 1st histogram using a transparent color
lines(x=nw_du$x,y=nw_du$y, col=3,lwd=3) # Add 2nd histogram using different color
rect(xleft = (2*pi)/32, xright = (2*pi)/6, ybottom = par("usr")[3], ytop = par("usr")[4], 
     border = NA, col = adjustcolor("blue", alpha = 0.3))
legend(1.25,0.05,c("local PCA unb", "constant PCA","business cycle band"), lwd=c(5,5), col=c(1,3, adjustcolor("blue", alpha = 0.3)), y.intersp=0.75)
dev.off()

